On 21 December 2012 06:30, Greg Aiken <gai...@visioninfosoft.com> wrote:
> #if one desires to pass a scalar reference into a sub-routine, > #the easiest way to assign a local scalar to the contents of the scalar > reference is... > subroutine(\$scalar); > sub subroutine { > my $subroutine_scalar = ${$_[0]}; #note you need the {} brackets, or > this doesn't work! > print "$subroutine_scalar\n"; > } > > #if one desires to pass an array reference into a sub-routine, > #the easiest way to assign a local array to the contents of the array > reference is... > subroutine(\@array); > sub subroutine { > my @subroutine_array = @{$_[0]}; #note you need the {} brackets, or > this doesn't work! > print "in subroutine: " . join(' ', @subroutine_array) . "\n"; > } > > #if one desires to pass a hash reference into a sub-routine, > #the easiest way to assign a local hash to the contents of the hash > reference is... > subroutine(\%hash); > sub subroutine { > my %subroutine_hash = %{$_[0]}; #note you need the {} brackets, or > this doesn't work! > print "in subroutine: " . join(' ', keys (%subroutine_hash)) . "\n"; > } > > all above works fine and is easy for me to understand. its below that im > having difficulty with... > > #seeing the 'pattern' of behavior for $, @, % variable types... > #i, not knowing any better, assumed the same should also be able to be > done for & (subroutines) > #i therefore tried a test to see if i could assign a new subroutine to > equal a de-referenced subroutine reference > #i literally copied the same code as used above, but used the & operator > instead of ($, @, %) > #this did not give the expected result... perl reported: > #hello CODE(0x237dbc) > #Can't modify non-lvalue subroutine call at D:\_junk\TEST.PL line 6. > > sub subroutine { > print "hello @_\n" > } > sub2(\&subroutine); > sub sub2 { > &sub3 = &{$_[0]}; #problem is obviously here with this line, seems its > not being dereference > sub3('world'); > } > It's more like: use strict; use warnings; sub subroutine { print "hello @_\n"; } sub2( \&subroutine ); sub sub2 { my $sub3 = shift; $sub3->('world'); } Typically you assign from subroutine parameters straight away rather than use @_ implicitly. Anyway, if you're on this path a recommended read is http://hop.perl.plover.com/ A definite cure for the seasonal hangovers . . . Just in
_______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs