reading files and scope of $_

I ran into something that I need help understanding. In the attached
script, subroutine file_proc1 converts all elements of [at] 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 [at] molec
and that of the <> operator, but i'd like to know what's happening so
I can avoid this in the future.

Thanks!

script:

#!/usr/bin/perl -w
use warnings;
use strict;
use Data::Dumper;

my %name_file = (
'foo1' => 'bar/bar1',
'foo2' => 'bar/bar2',
'foo3' => 'bar/bar3',
);

my [at] molec = keys(%name_file);
print Dumper(\ [at] molec);
dum_sub($name_file{$_}) foreach [at] molec;

file_proc2($name_file{$_}) foreach [at] molec;
print Dumper(\ [at] molec);

file_proc1($name_file{$_}) foreach [at] molec;
print Dumper(\ [at] molec);


sub file_proc1{
my $file = shift;
open my $input_fh, "<", $file or die "could not open $file \n";
while (<$input_fh>) {
#do something someday
}
close ($input_fh);
}

sub file_proc2{
my $file = shift;
open my $input_fh, "<", $file or die "could not open $file \n";

while (my $line = <$input_fh>) {
#do something someday
}

close ($input_fh);

}

sub dum_sub{
print " [at] _ $_\n";
}



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Demian [ Di, 22 Februar 2011 17:59 ] [ ID #2055832 ]

Re: reading files and scope of $_

D wrote:
> I ran into something that I need help understanding. In the attached
> script, subroutine file_proc1 converts all elements of [at] 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 [at] 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 [at] 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 ( [at] molec ) {
file_proc1( $name_file{ $element } );
}
print Dumper(\ [at] 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
jwkrahn [ Do, 24 Februar 2011 10:52 ] [ ID #2055840 ]
Perl » gmane.comp.lang.perl.beginners » reading files and scope of $_

Vorheriges Thema: Attachment in Excel format?
Nächstes Thema: baby code