On Apr 1, 2006, at 2:49 AM, kurtz le pirate wrote:
hello,
mac os x store file name in utf-8 format. so, how to open file with
"special" characters in name ?
a very simple exemple is a file name that begin with space. if i
write :
open(FILE," Read in a file"), perl return an error:
*** can't open [ Read in a file] : No such file or directory
That's just because open() trims whitespace from the front of the
argument. See the open() docs in perlfunc:
--------------
...
The filename passed to 2-argument (or 1-argument) form of open() will
have leading and trailing whitespace deleted, and the normal
redirection characters honored. This property, known as "magic open",
can often be used to good effect. A user could specify a filename of
F<"rsh cat file |">, or you could change certain filenames as needed:
$filename =~ s/(.*\.gz)\s*$/gzip -dc < $1|/;
open(FH, $filename) or die "Can't open $filename: $!";
Use 3-argument form to open a file with arbitrary weird characters in
it,
open(FOO, '<', $file);
otherwise it's necessary to protect any leading and trailing whitespace:
$file =~ s#^(\s)#./$1#;
open(FOO, "< $file\0");
...
--------------
The 3-argument form of open() is definitely preferred.
-Ken