Monday, June 27, 2011

Perl dereferencing

Reference in  perl:

$scalarref=\$foo;      # for  storing scalar variable reference
$constref=\182_5555.45    # for  storing constant reference
$arrayref=\@dddd;     # for  storing  array reference
$hashref=\%hash;      # for storing hash reference
$coderef=\&handler;     # for  storing subroutine reference
$globref=\*STDOUT;    # for  storing STDOUT reference
anonymous subroutine composer

$coderef=sub { print "hello!\n"};
$objref=new Doggie Tail=>'short',Ears=>'long';
$objref=Doggie->new(Tail=>'short',Ears=>'long');
Dereference:

for deferencing a scalar variable

$foo="two humps";
$scalarref=\$foo;
$camel-model=$$scalarref;
(or)
$bar=${$scalarref};

For deferencing the array 

 push(@$arrayref,$filename);
$$arrayref[0]="January";
(or)
push(@{$arrayref},$filename);
${$arrayref}[0]="January";
(or)
$arrayref->[0]="January";   # using the arrow operator

For deferencing the hash:

$$hashref{"key"}="Value";
(or)
${$hashref}{"KEY"}="VALUE";
(or)
$hashref->{KEY}="VALUE";  #using the arrow operator

For deferencing the sub-routine reference: 

&$coderef{1,2,3};
(or)
&{$coderef}{1,2,3};

For deferencing the global reference:

print $globalref "output\n";     # prints output to the standard output.