Sunday, July 17, 2011

Call by reference perl

When you call by reference ,you use directly the scalar reference and so any change in the function causes the change to happen in the main program.

Content of the perl file:

@array=("hello","aaaa","lll","ggg");
print "The content of array element 0 and 1 before function call is $array[0] and $array[1]"."\n";
function(@array);
print "The content of array element 0 and 1 after function call is $array[0] and $array[1]";
sub function()
{
$_[0]="Great";#this is the scalar reference of the first element in the array.
$_[1]="awesome";#this is the scalar reference of the second element in the array.
}

Output:

G:\perl_programs>perl perl_pass_by_reference.pl
The content of array element 0 and 1 befoe function call is hello and aaaa
The content of array element 0 and 1 after function call is Great and awesome
G:\perl_programs>

No comments:

Post a Comment