verdemar has asked for the wisdom of the Perl Monks concerning the following question:
Here's a simplified version of the code. The work_ sub was far away in a module and difficult to find then.
Hoping for some enlightenment on the nature of $_ ...
my @list = ( { a => 1 }, { a => 15 } ); # here the values of a in the hashrefs are changed, ok! work_( $_ ) for @list; print "_=".(ref $_ ? "r.$_->{a}" : $_) . "\n" for @list; # but here the hashrefs in @list are replaced by scalars, # namely the result of the work_() call. work_( $_, 1 ) for @list; print "_=".(ref $_ ? "r.$_->{a}" : $_) . "\n" for @list; sub work_ { my ($obj, $act) = @_; $obj->{a}++; $_ = $obj->{a} if $act; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: global $_ behavior
by dave_the_m (Monsignor) on Dec 02, 2005 at 12:14 UTC | |
by jonix (Friar) on Dec 02, 2005 at 16:06 UTC | |
by TimToady (Parson) on Dec 02, 2005 at 16:17 UTC | |
|
Re: global $_ behavior
by psychotic (Beadle) on Dec 02, 2005 at 11:26 UTC | |
by verdemar (Initiate) on Dec 02, 2005 at 13:15 UTC | |
by jonadab (Parson) on Dec 02, 2005 at 14:28 UTC |