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.

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 @molec = keys(%name_file);
print Dumper(\@molec);
dum_sub($name_file{$_}) foreach @molec;

file_proc2($name_file{$_}) foreach @molec;
print Dumper(\@molec);

file_proc1($name_file{$_}) foreach @molec;
print Dumper(\@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 "@_ $_\n";
}



-- 
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