At 15:49 +0200 2002.05.27, Bart Lateur wrote:
>Sorry for the late reply. Actually, no, I'm not sorry, I've been away
>for a few weeks, so it's actually not my fault. :-)
>
>On Mon, 13 May 2002 13:04:43 +0200, Axel Rose wrote:
>
>>At 14:37 Uhr +0100 06.05.2002, Alan Fry wrote:
>>>open(IN, $f);
>>
>>Problem 1:
>>open() failes if a filename contains spaces. This is a very
>>common problem. Even Net::FTP didn't work.
>>Everybody opening files from the Desktop has to make this
>>experience.
>
>Then use the three argument open(). It's been added to Perl in order to
>solve this very problem, for one. It's rather new, I'm pretty sure it
>wasn't in perl 5.004, but it *is* available in 5.6, so with the newest
>MacPerl, you should be able to use it.
>
>The way it works, is that instead of using a combined string with file
>name and opening mode, you now have them in separate arguments:
>
>       open(IN, "<", $f);
>
>Simple, isn't it?

Yes.

However, two notes:

* open() does *not* fail on a filename with spaces; it fails on a filename
with leading or trailing spaces.  This works fine, with embedded spaces:

  my $f = "Bourque:Desktop Folder:file.txt";
  open F, $f or die $!;
  print scalar readline F;

This does not:

  my $f = "Bourque:Desktop Folder:file.txt ";
  open F, $f or die $!;
  print scalar readline F;

There are many solutions, one of which is three-arg open.  Another is to
use sysread(), which is sometimes a pain.  The third is to specify your
open sign ('>', '<', etc.) and add a trailing null:

  my $f = "Bourque:Desktop Folder:file.txt ";
  open F, '<' . $f . "\0" or die $!;
  print scalar readline F;


The greater danger with C< open F, $f > is that the filename might begin
with a ">" or somesuch.  Both three-arg open, and the method above with
"\0", solve both problems; but the latter method works in any version of
perl.  I am not a big fan of three-arg open, but I have to admit it looks a
lot nicer.  :-)

-- 
Chris Nandor                      [EMAIL PROTECTED]    http://pudge.net/
Open Source Development Network    [EMAIL PROTECTED]     http://osdn.com/

Reply via email to