unclesmiffy has asked for the wisdom of the Perl Monks concerning the following question:

check this out. create a file in linux with a space at the end. open can't see it.
[root@s15211486 .AppleDouble]# ls * | cat -vet Journal-B $ [root@s15211486 .AppleDouble]# perl -e ' @x = glob("*") ; open(FILE,"$ +x[0]") or die("($x[0]) $!"); ' (Journal-B ) No such file or directory at -e line 1.

Replies are listed 'Best First'.
Re: open splutters and fails
by dave_the_m (Monsignor) on Jun 02, 2007 at 22:45 UTC
    The 2-arg form of open strips leading and trailing spaces from the filename. Use the 3-arg form instead:
    open FILE, '<', $x[0] or die ....

    Dave.

      The 2-arg form of open strips leading and trailing spaces from the filename.
      Except... for trailing whitespace: when the string ends in a null character ("\0") (optionally followed by more whitespace). The null character is stripped (or just ignored), but anything further inward, is left alone, including spaces.

      At least, that's how I recall it (not tested today).

      Nevertheless... thank goodness for 3 argument open, no?

      p.s.AFAIK, there no equivalent trick for the front of the filename.

        AFAIK, there no equivalent trick for the front of the filename.

        A bit outside of the original box but open FOO, "> ./ foo \0" works (replace with File::Spec magic to slightly increase portability. (:

        - tye        

Re: open splutters and fails
by Joost (Canon) on Jun 02, 2007 at 22:43 UTC
      The OP was using open with 2 arguments. Score another plus for the 3-argument variant.
Re: open splutters and fails
by FunkyMonk (Bishop) on Jun 02, 2007 at 22:48 UTC
    If you want to open A filename containing a space, try using the 3 argument open. That works fine on my Debian box.

    Earlier today, some other Monk posted something like:

    open FH, "< myfile" or die $!;

    Would you really like that to fail because there is no file called " myfile".

    You're much better off using open with 3 arguments, anyway.

      how wise, yes the 3 arg version works.