in reply to calling subroutine inside loop - ERROR - subroutine executes only once
with this code the output is:use strict; use warnings; my @corners; $|++; #get correct order of print and die (STDOUT STDERR) while(<DATA>){ $_=~ s/\s+/ / ; chop $_; push @corners, $_ ; print "\n@corners\n"; } my $corner; foreach(@corners){ $corner=$_; &loading_tsc; } sub loading_tsc{ print "DEBUG: loading_tsc received '@_'"; chdir($corner) or die "\n $!\n"; print "\n"; print `pwd`; print "\n"; } __DATA__ AAA BBB CCC DDD
Firstly onother way to get array filled with lines from a file:AAA AAA BBB AAA BBB CCC AAA BBB CCC DDD DEBUG: loading_tsc received '' No such file or directory
@corners = <DATA> ;
foreach my $corner(@corners){...
foreach my $corner(@corners){ chomp $corner; # we still need to chomp it.. &loading_tsc ($corner); # better use of a sub. }
use Cwd; my $start_dir = getcwd; ... sub loading_tsc{ print "DEBUG: loading_tsc received '@_'"; my $current_corner = shift; chdir($start_dir.'/'.$current_corner ) or die "\n $!\n"; print "\n"; print `pwd`; print "\n"; }
use File::Spec; .. my $current_dir = File::Spec->rel2abs('.'); .. #in the sub now you can do: my $path_to_go = File::Spec->catdir( $current_dir , $current_corner ); chdir $path_to_go or die "unable to chdir in $path_to_go\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: calling subroutine inside loop - ERROR - subroutine executes only once
by kaushik9918 (Sexton) on Dec 24, 2014 at 09:07 UTC |