tags:

views:

71

answers:

2

I am trying to output a file in perl.

I open the file and output like this..

open(my $out, ">",  "output.html") or die "Can't open output.txt: $!";
print $out "something!";

Which works perfect. If I change it to this

open(my $out, ">",  "c:\somedirectory\output.html") or die "Can't open output.txt: $!";
print $out "something!";

It does run fine(I do not get the 'Can't open output.txt' message) but when I look in the directory the file I just output isn't there. If I leave it with no path the file is found in the bin.

What am I missing here? How do I get it to output another location.

Also.. I am running the .pl using this .bat file.

cd\

cd \xampp\perl\bin

perl "C:\somedirectory\languages.pl"

pause
+4  A: 

You have to use double backslashes as directory separators for windows paths, or use simple forward slashes.

Otto Allmendinger
strange i don't get the 'Can't open output.txt' message. but thanks!
ctrlShiftBryan
@ctrlShiftBryan that's because it *is* successfully opening a file, just not where you're looking for it. Given time, you'll find the trash file you accidentally created. :)
hobbs
+10  A: 

Within double quotes, \ starts an escape sequence. Unrecognized escapes are simply ignored.

print "abc\tdef";
# abc     def
print "c:\somedirectory\output.html";
# c:somedirectoryoutput.html
print qq(c:\somedirectory\output.html);
# c:somedirectoryoutput.html

You may double up the backslashes in order to let them pass through the double quotes.

print "c:\\somedirectory\\output.html";
# c:\somedirectory\output.html
print qq(c:\\somedirectory\\output.html);
# c:\somedirectory\output.html

You may use single quotes instead of double quotes, because escape sequences are not recognized within single quotes.

print 'c:\somedirectory\output.html';
# c:\somedirectory\output.html
print q(c:\somedirectory\output.html);
# c:\somedirectory\output.html

And a final option: the Win32 and NT APIs happily treat / as directory separators just as well as \. While it may look odd, the following will work too:

open(my $out, ">", "C:/somedirectory/output.html");
ephemient
to be honest, I think backslashes look much odder
Otto Allmendinger