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.

In reply to Re: calling subroutine inside loop - ERROR - subroutine executes only once by Discipulus
in thread calling subroutine inside loop - ERROR - subroutine executes only once by kaushik9918

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.