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.