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'


how do you check the return code of system call?

System calls "traditionally" returns 9 when successful and 1 when it fails.

system (cmd) or die "Error in command";

What is '->' in Perl?

It is a symbolic link to link one file name to a new name. so lets say we do it like
file1-> file2, if we read file1, we end up reading file2.

Difference between 'chomp' and 'chop'?

'chop' functiononly removes the last character completely 'from the scaler, where as 'chomp' function only removes the last character if it is a newline. by default,

chomp only removes what is currently defined as the $INPUT_RECORD_SEPARATOR.
whenever you call 'chomp ', it checks the value of a special variable '$/'. whatever the value of '$/' is eliminated from the scaler. by default the value of '$/' is '\n'

Diffrence between 'use' and 'require' function?

Use:

1. the method is used only for modules (only to include .pm type file)
2. the included object are verified at the time of compilation.
3. No Need to give file extentsion.

Require:

1. The method is used for both libraries ( package ) and modules
2. The include objects are varified at the run time.
3. Need to give file Extension.

How do you know the reference of a variable whether it is a reference,scaller, hash or array?

Answer : There is a 'ref' function .

Perl provides the ref() function so that you can check the reference type before dereferencing a reference

The ref() function can return are:

Function CallReturn Value
ref( 10 );undefined
ref( \10);SCALAR
ref( \{1 => ; "Joe"} );HASH
ref(\&firstSub );CODE
ref( \\10);REF


Meaning of i and g in Oracle 9i & 10g

The i in oracle 8i and 9i stands for INTERNET and the g in 10g and 11g stands for GRID
bcz from 10g onwards oracle supports grid architecture.

The "i" and "g" Versions Starting in 1999 with Version 8i, Oracle added the "i" to the version name to reflect support for the Internet with its built-in Java Virtual Machine (JVM). Oracle 9i added more support for XML in 2001. In 2003, Oracle 10g was introduced with emphasis on the "g" for grid computing, which enables clusters of low-cost, industry standard servers to be treated as a single unit.