Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

using change directory within perl script

by cburger (Acolyte)
on Mar 27, 2012 at 03:51 UTC ( #961824=perlquestion: print w/ replies, xml ) Need Help??
cburger has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to change directories within a perl script, but for some reason it doesn't follow correctly into the right directory. Here is the code, if you know what's wrong with it - help is a lot appreciated! Thanks.

#!/usr/bin/perl use File::chdir; use Cwd; use strict; my $dir='/data1/sgb'; chdir '$dir/Hsapiens_fasta_folder'; my @files=<*.fa>; chdir '$dir/targetp-1.1'; my $current=getcwd(); foreach my $file( @files) { print "$file\n"; print "$current\n"; }

so what happens in this script is that instead of print at $current : chdir '$dir/targetp-1.1'; it prints '/data1/sgb'; - i.e. the location of $dir. I have no idea why. Thanks for the help.

Comment on using change directory within perl script
Download Code
Re: using change directory within perl script
by bms (Monk) on Mar 27, 2012 at 04:11 UTC

    Why are you using chdir with File::chdir?

    Try using it like:

    ... use File::chdir; $CWD = '/usr/bin'; # Oh hey, in /usr/bin/ ...

    Or

    ... use File::chdir; $CWD = '/home/me/perl'; # Now in /home/me/perl { local $CWD = '/home/you/python'; # Now in /home/you/python ... } # Back in /home/me/perl ...

    Hope this helps!

Re: using change directory within perl script
by jwkrahn (Monsignor) on Mar 27, 2012 at 04:16 UTC
    use File::chdir;

    Did you read the documentation for this module?    You are not using it anywhere in this program.



    chdir '$dir/Hsapiens_fasta_folder'; ... chdir '$dir/targetp-1.1';

    You should verify that these functions worked correctly:

    chdir '$dir/Hsapiens_fasta_folder' or warn "Cannot chdir to '$dir/Hsap +iens_fasta_folder' because: $!"; ... chdir '$dir/targetp-1.1' or warn "Cannot chdir to '$dir/targetp-1.1' b +ecause: $!";
Re: using change directory within perl script
by tangent (Curate) on Mar 27, 2012 at 04:22 UTC
    The variable $dir is not interpolated when you use single quotes, try changing them to doubles: "$dir/Hsapiens_fasta_folder"

    The idea of File::chdir is to not use chdir() and use $CWD instead:
    #!/usr/bin/perl use strict; use File::chdir; my $dir = '/data1/sgb'; $CWD = "$dir/Hsapiens_fasta_folder"; my @files=<*.fa>; $CWD = "$dir/targetp-1.1"; foreach my $file( @files) { print "$file\n"; print "$CWD\n"; }
      ThANKS A LOT ...VERY HELPFUL

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://961824]
Approved by tobyink
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2014-12-22 21:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (132 votes), past polls