Hi All,
Could you look at the below script and help me understand why the output is in reverse order.
Code:
lab@a128:~/mydir> cat ref2fn
#!/usr/bin/perl
# Function definition
sub PrintHash {
my (%hash) = @_;
foreach $item (%hash) {
print "Item: $item\n";
}
}
%hash = ('name' => 'Tom', 'age' => 19);
%hash1 = ('name' => 'Tommy', 'age' => 39, 'location' => 'Utah');
#Create a reference to above function.
$cref = \&PrintHash; #Uncomment after test
$cref1 = &PrintHash(%hash1);
print "Valud of \$cref is: $cref\n";
#Function call using reference.
&$cref(%hash); #Uncomment after test
&$cref1(%hash1); #Uncomment after test
lab@a128:~/mydir> ./ref2fn
Item: location
Item: Utah
Item: name
Item: Tommy
Item: age
Item: 39
Valud of $cref is: CODE(0x7b3f20)
Item: name
Item: Tom
Item: age
Item: 19
Undefined subroutine &main:: called at ./ref2fn line 20.
lab@a128:~/mydir>
I was expecting the below output to print first
Code:
Item: name
Item: Tom
Item: age
Item: 19
Why is this so? Please explain.
Also, it's complaining
Code:
Undefined subroutine &main:: called at ./ref2fn line 20.
in the last line of the output. What is it complaining about?
Thanks in advance!! Appreciate your time.