Saturday, July 2, 2011

ENV in perl to view the environmental variables set


The program to view the environmental variables set:
foreach $key (keys(%ENV)) {
    printf("%-10.10s: $ENV{$key}\n", $key);
}

ouput:

HOMEPATH  : \Documents and Settings\acer
PATH      : C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Pr
iles\QuickTime\QTSystem\;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;
wberry\perl\bin
USERDOMAIN: ACER-5D58E53AB3
TERM      : dumb
PROCESSOR_: x86
QTJAVA    : C:\Program Files\Java\jre1.6.0_02\lib\ext\QTJava.zip
TEMP      : C:\DOCUME~1\acer\LOCALS~1\Temp
PROCESSOR_: 2502
SYSTEMDRIV: C:
SYSTEMROOT: C:\WINDOWS
COMSPEC   : C:\WINDOWS\system32\cmd.exe
LOGONSERVE: \\ACER-5D58E53AB3
SESSIONNAM: Console
FTP_PASSIV: 1
WINDIR    : C:\WINDOWS
PROCESSOR_: 6
USERNAME  : acer
PROCESSOR_: x86 Family 6 Model 37 Stepping 2, GenuineIntel
ALLUSERSPR: C:\Documents and Settings\All Users
COMPUTERNA: ACER-5D58E53AB3
CLASSPATH : .;C:\Program Files\Java\jre1.6.0_02\lib\ext\QTJava.zip
TMP       : C:\DOCUME~1\acer\LOCALS~1\Temp


ENV in perl to add path


ENV is a hash in perl.
You can access as it a simple hash
For instance to add a path in the environment variable PATH in the OS execute the following program:

env_path.pl:
$ENV{'PATH'} = "G:\\perl_programs;"."$ENV{'PATH'}";
print "$ENV{'PATH'}";


output:
G:\perl_programs>perl env_path.pl
G:\perl_programs;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Prog
ram Files\QuickTime\QTSystem\;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;C:
\strawberry\perl\bin
G:\perl_programs>

This added path will go off once the perl program is executed.It will be available only to the child process

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.