Sunday, September 18, 2011

The caller() function and $wantarray

The argument of interest is the $wantarray argument. This indicates what return value is
expected of the subroutine from where it was called. The subroutine could have been
called in void context meaning the return value is thrown away. Or it could have been
called and the return value assigned to a scalar. Or it could have been called and the
return value assigned to a list of scalars.
sub CheckMyWantArray
{
my @info = caller(0);
my $wantarray = $info[5];
$wantarray='undef'
unless(defined($wantarray));
print "wantarray is '$wantarray'\n";
}
CheckMyWantArray; # undef
my $scal = CheckMyWantArray; # 0
my @arr = CheckMyWantArray; # 1
> wantarray is 'undef'
> wantarray is '0'
> wantarray is '1'

Using the caller() Function in Subroutines

The caller() function can be used in a subroutine to find out information about where the
subroutine was called from and how it was called. Caller takes one argument that
indicates how far back in the call stack to get its information from. For information about
the current subroutine, use caller(0).
sub HowWasICalled {
my @info = caller(0);
print Dumper \@info;
}
HowWasICalled;
>$VAR1 = [
> 'main',
> './test.pl',
> 13,
> 'main::HowWasICalled',
> 1,
> undef,
> undef,
> undef,
> 2,
> 'UUUUUUUUUUUU'
> ];

The caller() function returns a list of information in the following order
0 $package package namespace at time of call
1 $filename filename where called occurred
2 $line line number in file where call occurred
3 $subroutine name of subroutine called
4 $hasargs true if explicit arguments passed in
5 $wantarray list=1, scalar=0, void=undef
6 $evaltext evaluated text if an eval block
7 $is_require true if created by "require" or "use"
8 $hints internal use only, disregard
9 $bitmask internal use only, disregard

Implied Arguments in perl

When calling a subroutine with the "&" sigil prefix and no parenthesis, the current @_
array gets implicitely passed to the subroutine being called. This can cause subtly odd
behaviour if you are not expecting it.
sub second_level {
print Dumper \@_;
}
sub first_level {
# using '&' sigil and no parens.
# doesn't look like I'm passing any params
# but perl will pass @_ implicitely.
&second_level;
}
first_level(1,2,3);

> $VAR1 = [
> 1,
> 2,
> 3
> ];

Wednesday, September 14, 2011

what is it meants by '$_'?

It is a default variable which holds automatically, a list of arguements passed to the subroutine within parentheses.


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 and cannot gets its visibility inherited functions called within that block, but one defined as 'local' can live within the block and have its visibility in the functions called within that block.


what is meant by a 'pack' in perl?

Pack Converts a list into a binary representation. Takes an array or list of values and packs it into a binary structure, returning the string containing the structure It takes a LIST of values and converts it into a string. The string contains a concatenation of the converted values. Typically, each converted values looks like its machine-level representation. for example, on 32-bit machines a converted integer may be represented by a sequence of 4 bytes.

How to substitute a particular string in a file containing million of record?

perl -p -ibak -e 's/search_str/replace_str/g'