Perl chdir error

1.3k Views Asked by At

I am trying to change the working directory (for configure a WebShpere MQ Queue manager) using Perl in UNIX.

I have to go to the directory /var/mqm/qmgrs/Q\!MAN and I have used following code snippet:

$QueueManagerPathName = 'Q\!MAN';
chdir('/var/mqm/qmgrs/'.$QueueManagerPathName) or die "Cannot change to dir : /var/mqm/qmgrs/".$QueueManagerPathName."\n";

But it does not change the directory and dies giving

Cannot change to dir : /var/mqm/qmgrs/Q\!MAN

When i remove the variable $QueueManagerPathName its working fine and it concludes me that it would be error using "\!" part.

3

There are 3 best solutions below

4
On BEST ANSWER

You don't need to escape ! in the directory name. This should work:

my $dir = '/var/mqm/qmgrs/Q!MAN';
chdir $dir or die "Can't cd to $dir: $!\n";
0
On

You have a backslash in your variable... I'd say that you are doing to much escaping.

0
On

Single quoted strings do not interpolate backslash, so you're trying to change to a directory called /var/mqm/qmgrs/Q\!MAN

Either omit the backslash, or use a double-quoted string.