in reply to Foreach & scalar ref: break of way that it is suposed to work!
Try this:
and note that it producesmy %hash = ( key => 'value'); my $k_ref = \$hash{key} ; *{'main::val'} = $k_ref ; ## To avoid $$k_ref print "val: <$val>\n" ; print "key: <$hash{key}>\n" ; { local( $val )= 'test' ; print "val: <$val>\n" ; print "key: <$hash{key}>\n" ; }
and you might see the light.val: <value> key: <value> val: <test> key: <value>
foreach does an implicit local which does a rough equivalent of
That is, it copies off and saves the reference to the variable rather than the value contained in the variable. - tyemy $saved= *main::val{SCALAR}; *main::val= do { my $new; \$new }; ... *main::val= $saved;
|
|---|