-2

I am trying to create a directory using Perl. But this call fails. However when I try to create the same directory structure in shell prompt, it works fine. Could someone please let me know why I am not able to create the directory in the directory structure? Example:

$absolutepath = "/localdatafs1/Domino/mail\abhy.nsf/Sent/Metadata";
print $absolutepath."\n";
mkdir "$absolutepath" or die $!;

In this example, localdatafs1, Domino, mail\abhy.nsf, and Sent are directories that already exist. I want to create a directory called metadata in the directory structure /localdatafs1/Domino/mail\abhy.nsf/Sent/Metadata using Perl. This mkdir call fails.

If I execute the command

mkdir /localdatafs1/Domino/mail\abhy.nsf/Sent/Metadata

in shell prompt, the directory gets created successfully.

Why I am unable to create the directory in Perl using the above path?

ikegami
  • 367,544
  • 15
  • 269
  • 518
Rajath
  • 215
  • 5
  • 13
  • 1
    Is your path supposed to contain the `\a` character? – Tim Oct 18 '11 at 20:11
  • 3
    What does the `die` message tell you? – Brian Roach Oct 18 '11 at 20:11
  • It looks like you have a "\" instead of a "/" between "mail" and "abhy.nsf" Have you tried swapping the direction of your "\"? – John Mark Oct 18 '11 at 20:13
  • @TimN: The path should contain the \a. – Rajath Oct 18 '11 at 20:18
  • @BrianRoach: die message says No such file or directory at ./transfer.pl – Rajath Oct 18 '11 at 20:19
  • @JohnMark: The directory that I need to create should be like mail\abhy.nsf. I need '\'. – Rajath Oct 18 '11 at 20:20
  • The post has been edited to the point of removing the problem. The supposedly working `mkdir` cannot possibly work. Reverted those edits. – ikegami Oct 18 '11 at 20:22
  • @ikegami - sorry, didn't notice the indenting changed the escaping, I hadn't actually changed his original text. He had used `\\` to make the backslash appear in the original post apparently. – Brian Roach Oct 18 '11 at 20:29
  • @ikegami: I am confused. The path that i want to create is "/localdatafs1/Domino/mail\abhy.nsf/Sent/Metadata". After mail, 2 back slashes are there in my code to escape a backslash. SO the path in the code is "/localdatafs1/Domino/mail\\abhy.nsf/Sent/Metadata". The directory named"mail\abhy.nsf" is already there in the system. – Rajath Oct 18 '11 at 20:41
  • @BrianRoach Now if the try creating the folder using mkdir "/localdatafs1/Domino/mail\\abhy.nsf/Sent/Metadata" in the perl code, it fails. However the same command mkdir "/localdatafs1/Domino/mail\\abhy.nsf/Sent/Metadata" passes. – Rajath Oct 18 '11 at 20:42
  • If you use `$absolutepath = "/localdatafs1/Domino/mail\\abhy.nsf/Sent/Metadata";` you shouldn't get `No such file or directory`. – ikegami Oct 18 '11 at 21:40
  • What's the output of `use Data::Dumper; $Data::Dumper::Useqq = 1; print(Dumper($absolutepath));` IMPORTANT: Copy the entire response, and place it in backtickets (`\``). – ikegami Oct 18 '11 at 21:43

2 Answers2

4

Your shell understands a different language than Perl. In your shell, the code

/localdatafs1/Domino/mail\abhy.nsf/Sent/Metadata

produces the string

/localdatafs1/Domino/mail\abhy.nsf/Sent/Metadata

In Perl, the code

"/localdatafs1/Domino/mail\abhy.nsf/Sent/Metadata"

produces the string

/localdatafs1/Domino/mail?bhy.nsf/Sent/Metadata

where the ? represents a non-printable control character. The Perl code

"/localdatafs1/Domino/mail\\abhy.nsf/Sent/Metadata"

produces the desired string. Note the escaped "\".

ikegami
  • 367,544
  • 15
  • 269
  • 518
3
$path = "/localdatafs1/Domino/mail\abhy.nsf/Sent/Metadata"
                                  ^--- escape character, turning the path into

$path = "/localdatafs1/Domino/mail".chr(1)."bhy.nsf/Sent/Metadata"
ikegami
  • 367,544
  • 15
  • 269
  • 518
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 1
    Or you can use `mail\\abhy.nsf` if you actually want a backslash in the name, but that's generally not a good idea. – Keith Thompson Oct 18 '11 at 20:20
  • I have used the directory structure as "/localdatafs1/Domino/mail\\abhy.nsf/Sent/Metadata". mkdir "/localdatafs1/Domino/mail\\abhy.nsf/Sent/Metadata" is getting failed. – Rajath Oct 18 '11 at 20:22
  • @Rajath: yes, becaues `\\` is not a directory separator on Unix/Linux systems. You can't mix / and \ for paths in Unix, they have to be / – Marc B Oct 18 '11 at 20:25