2

I have two strings: $dir1 and $dir2. I want to use chdir as shown below:

chdir "/home/$dir1/$dir2";

when I try this it doesn't change the directory. I checked the current working directory same as before

Is there any way to do this?

techie
  • 59
  • 5

1 Answers1

4

Typically I write that as:

use v5.10;
use File::Spec::Functions;

my $dir = catfile( '/home', $dir1, $dir2 );
die "Dir <$dir> isn't a directory or doesn't exist" unless -e -d $dir;
chdir $dir or die "Could not change to <$dir>: $!";

Whenever you do something with the system, check the results to ensure it happened.

And, curiously, I didn't realize that Pearson's sample chapter of my book Effective Perl Programming is the one that covers stacked file test operators.

brian d foy
  • 121,466
  • 31
  • 192
  • 551