>> Make global variables ,same as using "our" before Variable Names..
2*What's the difference between dynamic and lexical (static) scoping? Between local() and my()?
local($x) saves away the old value of the global variable $x and assigns a new value for the
duration of the subroutine which is visible in other functions called from that subroutine. This is
done at run-time, so is called dynamic scoping. local() always affects global variables, also
called package variables or dynamic variables.
my($x) creates a new variable that is only visible in the current subroutine. This is done at
compile-time, so it is called lexical or static scoping. my() always affects private variables, also
called lexical variables or (improperly) static(ly scoped) variables.
For instance:
sub visible {
print "var has value $var\n";
}
sub dynamic {
local $var = 'local'; # new temporary value for the still-global
visible(); # variable called $var
}
sub lexical {
my $var = 'private'; # new private variable, $var
visible(); # (invisible outside of sub scope)
}
$var = 'global';
visible(); # prints global
dynamic(); # prints local
lexical(); # prints global
Notice how at no point does the value "private" get printed. That's because $var only has that
value within the block of the lexical() function, and it is hidden from called subroutine.
In summary, local() doesn't make what you think of as private, local variables. It gives a global
variable a temporary value. my() is what you're looking for if you want private variables.
See "Private Variables via my()" in perlsub and "Temporary Values via local()" in perlsub for
excruciating details.
3*How to embed data in your Perl program
Here's a sample Perl program that demonstrates how you can include (embed) data inside of
your Perl program, right in there next to the source code.
This simple program takes the data after the special __END__ tag, and makes it available to
your Perl source code.
#!/usr/bin/perl
while (
{
print $_;
}
__END__
George Washington
Abraham Lincoln
John F. Kennedy
4*What is true and false in Perl?
Perl is a little unusual in not having true and false boolean operators. Because of this, and my
advancing age, I can never seem to remember what equates to true and false when using Perl,
so I decided to create this page.
True/false summary
In short, the following elements in Perl will equate to "false":
* The number zero (0) means false.
* The string zero ('0') means false.
* The empty string ('') means false.
Lots of other things equate to "true", including:
* Non-zero numbers
* Non-empty strings
5.Explain the difference between "my" and "local" variable scope declarations. ?
Both of them are used to declare local variables.
The variables declared with "my" can live only within the block it was defined and cannot get its
visibility inherited functions called within that block, but one defined with "local" can live within
the block and have its visibility in the functions called within that block.
6.E-val with its own interpreter >Make it Fail to Throw Errors..
The eval function provides a very simple way of checking certain events without
affecting the overall execution of your script.
In essence the eval function just initiates a new instance of the Perl interpreter in order to
evaluate a particular string or block.
Because eval evaluates a Perl statement or block within its own interpreter we can use it in
situations that might otherwise cause the Perl interpreter to fail.
This process works because an embedded eval block reports any errors raised by a call to die
through the $@ variable. In fact any exit is reported through eval to the $@ special variable. We
can demonstrate this with a simple eval block used to test the existence of a particular module:
eval
{
require Net::FTP;
}
print Error: Module failed to load ($@) if $@;
This outputs the following:
$ perl eval.pl
Failed to load Net::FTP: Can't locate Net/LICK.pm in @INC (@INC contains:
/usr/local/lib/perl5/5.6.0/i686-linux /usr/local/lib/perl5/5.6.0
/usr/local/lib/perl5/site_perl/5.6.0/i686-linux
/usr/local/lib/perl5/site_perl/5.6.0 /usr/local/lib/perl5/site_perl .) at
eval.pl line 1.
7. How to substitute a particular string in a file containing millions of records?
perl -pi -e "s/search_string/replace_string/g" SourceFile
-P
causes your program to be run through the C preprocessor before compilation by Perl.
(Because both comments and cpp directives begin with the # character, you should avoid
starting comments with any words recognized by the C preprocessor such as ``if'', ``else'', or
``define''.)
-i
-e commandline
may be used to enter one line of program. If -e is given, Perl will not look for a filename in the
argument list. Multiple -e commands may be given to build up a multi-line script. Make sure to
use semicolons where you would in a normal program.
-T
forces ``taint'' checks to be turned on so you can test them. Ordinarily these checks are
done only when running setuid or setgid. It's a good idea to turn them on explicitly for programs
that run on behalf of someone else whom you might not necessarily trust, such as CGI programs
or any internet servers you might write in Perl
8.What's the significance of @ISA, @EXPORT @EXPORT_O...??
@ISA -> each package has its own @ISA array. this array keep track of classes it is inheriting.
ex:
package child;
@ISA ( parentclass);
@EXPORT this array stores the subroutins to be exported from a module.
@EXPORT_OK this array stores the subroutins to be exported only on request.
9.Special Variables in Perl:
Go through This Link : Special Variables in Perl
10.where do you go for perl help?
You can use the command
-> perldoc -f function name
Ex: perldoc -f print
Shell Scripting Tip:
* Example for using getopts:
getopts => getting options to correct var..
while getopts t:r: MYOPTION
do
case MYOPTION in
t) RESTAURANTTYPE=$OPTARG ;;
r) RATING=$$OPTARG ;;
\?) echo "Sorry no such option, please try again"
exit 1
;;
done
// myprog -t North -r 35 or myprog -r 35 -t North
Tutorial for using getopts...[click]
No comments:
Post a Comment