D wrote:
I ran into something that I need help understanding.  In the attached
script, subroutine file_proc1 converts all elements of @molec to
undef, while file_proc2 does not.  adjusting file_proc1 to first slurp
the file into an array "fixes it".  My best guess (via dum_sub) is
that the subroutine is crossing the $_ from the processing of @molec
and that of the<>  operator, but i'd like to know what's happening so
I can avoid this in the future.

$_ is a *global* variable so when the "while (<$input_fh>)" loop ends in the 'file_proc1' subroutine the value of undef is stored in $_ and that then affects the contents of @molec in the foreach loop. This is called "action at a distance" and is why it is recommended that you use explicit lexical variables like in the 'file_proc2' subroutine and probably also in the foreach loop, e.g.:

foreach my $element ( @molec ) {
    file_proc1( $name_file{ $element } );
    }
print Dumper(\@molec);



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to