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.

Friday, June 24, 2011

sending arrays as arguments in perl

Here we will learn how to send a array as argument in perl.
Here while sending data the reference of the array has to be sent .In the receiving end in the function we need to use special form of shift one of the below:

1)my @a = @{shift()};
2)my @b = @{shift()};
Simple shift can't be used here as it will shift only the scalar content;
Program:

sub myfunc
{
my @a = @{shift()};   # here @{+shift};  can also be used
print "@a";
}
package main;
@aa=("hello","aaaaa");
&myfunc (\@aa);  # take care here reference is used.

output:

hello aaaaa

function call in perl

There are three ways to call function in perl:

sub myfunc {  .  .  .  }
1)&myfunc(1,2,3);                old  style
2)myfunc(1,2,3);                  No ampersand
3) myfunc 1 , 2 , 3;                Works only if myfunc has already been declared.

Sample program:


1)package main;
$aa="hello";
$bb="aaaaa";
&myfunc ($aa,$bb);      #  in this case the subroutine can be defined before or after the function call;
sub myfunc
{
@aa[0]=shift ;
@aa[1]=shift;
print "@aa";
}

output :

hello aaaaa
2)

sub myfunc         # @_ is the default array where all the values are stored
{
@aa[0]=shift ;      # values are shifted
@aa[1]=shift;
print "@aa";
}
package main;
$aa="hello";
$bb="aaaaa";
myfunc($aa,$bb);       #  in this case the subroutine can be defined before or after the function call;

output:

hello  aaaaa
3)In the third case the disadvantage is when u try to call the function without the defining the function in the preceding code it will return error.


package main;
$aa="hello";
$bb="aaaaa";
myfunc $aa,$bb;   # here the myfunc not defined in the preceeding lines.
sub myfunc
{
@aa[0]=shift ;
@aa[1]=shift;
print "@aa";
}

output:

G:\perl_programs>perl function_perl.pl
Can't locate object method "myfunc" via package "hello" (perhaps you forgot to l
oad "hello"?) at function_perl.pl line 5.

It works if used like this:


sub myfunc
{
@aa[0]=shift ;
@aa[1]=shift;
print "@aa";
}
package main;
$aa="hello";
$bb="aaaaa";
&myfunc ($aa,$bb);

output:

hello aaaaa

$_(default) in perl

$_ is the default argument
It is the default argument for many operators and also some control structures.
$_ is a normal scalar variable and it can be used as a normal scalar variable,you can change it.print it and use it.
Some of the common usuages of $_ and how the default operator is automatically interpreted in perl :


print $_;
print;                        Same result

print "found it" if  $_ =~ /Rosebud/;
print  "found  it " if  /Rosebud/;               Same result


foreach $_(@list) { &do_something($_) };e
foreach (@list){  &do_something($_) };       Same result


while (defined($_ = <STDIN>)){ print $_; }
while(<STDIN>){ print ; }                                 Same result

$_ always belongs to the main package.
Even if you define $_ in some other package it will default always to $main::$_


package hello;
$_="hai";
package main:
print $_;       #  it prints hai
Here hai as print $_;  is same as --->  print $main::$_;


You can even use $hello::$_ and assign it some value.print it.But this is not same as $_ and it has no special property.

Tuesday, June 21, 2011

perl check true or false

In perl  0 and "" are considered as false and all others are considered as true.

In Perl for checking the variable  is assigned , you should not check the value returned is false,as it may fail in some cases.

Consider for example:

if ($variable)
{
          print "valuse present";
}
else
{
         print "value not present"
}

This check will fail tremendously when $variable is equal to 0


The correct way to do it is


while (defined ($variable)){
          print "valuse present";
}
else
{
         print "value not present"
}

In case of hash to check if the element is present in the hash.

In this case the exists operator has to be used.


if (exists($hash{'foo'})) {      }   TRUE

In this case if you use defined function it will always return true.

perl true false

In perl  0 and "" are considered as false and all others are considered as true.

In Perl for checking the variable  is assigned , you should not check the value returned is false,as it may fail in some cases.

Consider for example:

if ($variable)
{
          print "valuse present";
}
else
{
         print "value not present"
}

This check will fail tremendously when $variable is equal to 0


The correct way to do it is


while (defined ($variable)){
          print "valuse present";
}
else
{
         print "value not present"
}

In case of hash to check if the element is present in the hash.

In this case the exists operator has to be used.


if (exists($hash{'foo'})) {      }   TRUE

In this case if you use defined function it will always return true.

Comparison in perl

Perl has two different set of comparison operators for both numbers and string.

For string we need to use only the following operators
In case of string the comparison takes place ASCIIbetically -that is by comparing ASCII values of the characters in Strings.


'a' lt 'b'         TRUE
'a' eq 'A'      FALSE-capitalization
"joseph" eq "joseph "  FALSE-spaces count
"H"  cmp "He"  -1--cmp operator

The cmp operator does a string comparison and returns -1,0,1 depending on the left argument is greater than,less than the right argument.
This operator should not be used for string comparison


"aaaaaaaa"  == "fgdfgdfgdf"    TRUE always as for equal to operator the string always looks to be 0

Numeric comparison use the following:

0<5                     TRUE
10 == 10.0          TRUE
10 <=> 9.5          1     --->spaceship operator
The spaceship operator is like cmp for strings except it compares numerically.
Should not use string comparators for numeric.

'10' gt '2'               FALSE -  '1' sorts before 2
"10.0" eq "10"       FALSE-different strings