Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

check if chdir() has work

by steph_bow (Pilgrim)
on Mar 14, 2008 at 14:59 UTC ( [id://674217]=perlquestion: print w/replies, xml ) Need Help??

steph_bow has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Could you tell me if there is a command to check if chdir() has worked ? Because I browse through many directories and I would like to know if I have indicated the good path. Thanks a lot

Replies are listed 'Best First'.
Re: check if chdir() has work
by Corion (Patriarch) on Mar 14, 2008 at 15:02 UTC

    I wonder if you'd care to suggest how we could improve the documentation for chdir to make it more obvious how to solve your problem. Currently the docs say this:

    It returns true upon success, false otherwise. See the example under die.
Re: check if chdir() has work
by dwm042 (Priest) on Mar 14, 2008 at 15:28 UTC
    The classic programming trope for testing chdir is:

    chdir $my_dir or die("Cannot cd into directory $my_dir.\n");
    It's a moderately important trope as well. At a former employer, they had shell code that went something like:

    #!/bin/ksh Important_Directory=/foo/bar cd Important_Directory rm -rf * # # more stuff here #
    See, when chdir fails, and you're root, you end up in the root directory. And the server on which this script was run, well the Important Directory was moved for some reason. And so the script cd's to "/" and the next command started running..

      You will want to include $! somewhere in your error message, so you'll know why chdir has failed.
Re: check if chdir() has work
by moritz (Cardinal) on Mar 14, 2008 at 15:10 UTC
    Of course Corion's suggestion is the best, but if you still mistrust the return value (there's no reason, mind) you can still compare the current dirctory (see Cwd) to chdir's target value.
(Re: check if chdir() has work) Savoir que chdir aura réussi
by Narveson (Chaplain) on Mar 15, 2008 at 13:06 UTC
    use Fatal 'chdir'; # et plus tard my $répertoire_voulu = 'fou/barre'; chdir $répertoire_voulu; # si le répertoire courant n'est pas $répertoire_voulu # le programme sera déjà mort unlink glob '*.log';

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-20 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found