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.
| [reply] |
| [reply] |
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..
| [reply] [d/l] [select] |
You will want to include $! somewhere in your error message, so you'll know why chdir has failed.
| [reply] [d/l] |
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. | [reply] |
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';
| [reply] [d/l] |