Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: calling subroutine inside loop - ERROR - subroutine executes only once

by Discipulus (Canon)
on Dec 24, 2014 at 08:19 UTC ( [id://1111283]=note: print w/replies, xml ) Need Help??


in reply to calling subroutine inside loop - ERROR - subroutine executes only once

hello kaushik9918 and welcome.

Your code is full of strange constructs and even if i'm not a master, i can suggest you some more perlish Perl.

First a slightly modified version of your code:
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
with this code the output is:
AAA AAA BBB AAA BBB CCC AAA BBB CCC DDD DEBUG: loading_tsc received '' No such file or directory
Firstly onother way to get array filled with lines from a file:
@corners = <DATA> ;

then declare your var within the loop without juggling with $_:
foreach my $corner(@corners){...


More on when you call your subroutine you pass no argguments to it but you modify a global variable inside it. is not the safer way:

foreach my $corner(@corners){ chomp $corner; # we still need to chomp it.. &loading_tsc ($corner); # better use of a sub. }


Now the chdir-path part: when you deal with path is better have an absolute one to use. In fact assuming you run your original code frome the dir /var/tests you get, as the first item of the array is processed, chdir AAA and if it is succesful you now are in /var/tests/AAA in the next iteration you try to go in /var/tests/AAA/BBB while you probably want /var/tests/BBB.

Better would be to record the starting directory using the core Cwd module and concatenate the path using this base, as in:
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"; }

This probably works as you wanted, but is still no safe Perl: when dealing with paths you need to be sure to the right thing possibly for every OS you can encounter. And concatenate paths with '/' can also be annoying. The core File::Spec is handy in this case:
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";


You type some chars more but you can be sure to do the right thing and have a more robust code.

HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

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
    Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1111283]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-19 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found