From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Greg Aiken Sent: 20 December 2012 22:31 To: Perl-Win32-Users@listserv.ActiveState.com Subject: learning references/dereferencing (understand $,@,%, but trouble understanding &)
> #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... Depends. Sometimes it is easier store the reference locally, and dereference as needed. See below. > subroutine(\$scalar); > sub subroutine { > my $subroutine_scalar = ${$_[0]}; #note you need the {} brackets, or this > doesn't work! > print "$subroutine_scalar\n"; my $ref = shift; print "$$ref\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"; my $ref = shift; print "in subroutine: @$ref\n"; print " first entry is $ref->[0]\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"; my $ref = shift; print "$_ = $ref->{$_}\n" foreach keys %$ref; > } > > 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'); The only variable types are scalar array and hash. There is no subroutine variable type, but you can store a reference to a subroutine, which is a scalar, in a variable. You dereference it when you want to call it. my $ref = shift; $ref->('world'); > } For more detail see 'perldoc perlref' HTH -- Brian Raven ________________________________ Please consider the environment before printing this e-mail. This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden. _______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs