开发者

Perl: How do I write to a directory besides bin?

开发者 https://www.devze.com 2022-12-21 17:52 出处:网络
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 ou开发者_如何学JAVAtput.txt: $!\";

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 ou开发者_如何学JAVAtput.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


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");


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

0

精彩评论

暂无评论...
验证码 换一张
取 消