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

Perl's  open seems to behave differently in UNIX and Windows, and the behavior under Windows is *odd*, or I am doing something dumb. This script:
#!/usr/local/perl-5.6.1/bin/perl use strict; $|=1; eval { open (FOO,">/tmp/tmp/tmp/tmp") ||die print FOO "hello"; close FOO; }; if ($@) { print "open failed $@"; } me@unixsystem:/tmp# ./fp.pl open failed Died at ./fp.pl line 6.
This fails like I thought it should because /tmp/tmp/tmp does not exist, and my message is printed. The directory needs to be made before we can create files there. Under Windows, similar code:
use strict; $|=1; eval { open (FOO,">c:\gpp\gpp\gpp") ||die; print FOO "hello"; close FOO; }; if ($@) { print "open failed $@"; }
Runs successfully (does not print error message) when the c:\gpp\gpp directory does not exist. It does not create any file either. If I try and open a file on an q:\gpp\gpp (in this case an unmapped drive), it does actually die and trigger my error message.
Does anyone know what the problem is here?

Rohit

Replies are listed 'Best First'.
Re: inconsistency with ActiveState's open?
by dreadpiratepeter (Priest) on Jul 14, 2003 at 14:50 UTC
    Since you didn't escape your backslashes in the double quoted string, I would guess that is created a file called gppgppgpp in your whatever directory you rand the program in (or c:\ i'm not sure).
    Try "c:\\gpp\\gpp\\gpp"
    Update: using warnings would have told you that an unknown escape \g was being used.

    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: inconsistency with ActiveState's open?
by Thelonius (Priest) on Jul 14, 2003 at 14:51 UTC
    If I run it with perl -w (ActiveState 5.8), I get:
    Unrecognized escape \g passed through at testfile line 3. Unrecognized escape \g passed through at testfile line 3. Unrecognized escape \g passed through at testfile line 3.
    You need to either use single quotes
    open (FOO,'>c:\gpp\gpp\gpp') ||die;
    or escape the backslashes
    open (FOO,">c:\\gpp\\gpp\\gpp") ||die;
    or use forward slashes:
    open (FOO,'>c:/gpp/gpp/gpp') ||die;
Re: inconsistency with ActiveState's open?
by davorg (Chancellor) on Jul 14, 2003 at 14:52 UTC

    The backslash has a special meaning in double-quoted strings. Either replace the quotes with single quotes, or replace the backslashes with forward slashes.

    You'll probably find your file at C:gppgppgpp.

    Update: tye is (of course) completely right below. I was more than a little confused when writing this.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      The backslash has a special meaning in double-quoted strings.

      Um, the backslash has a special meaning in single-quoted strings as well. It has a special meaning in almost all Perl quote(-like) situations1 (<<'END' and qw() being the only exceptions).

      The reason you can get away with 'c:\ggp' is because single-quoted strings leave unrecognized escapes as-is (while double-quoted strings replace unrecognized escapes with just the escaped character).

      But if you get in the habbit of writing 'c:\ggp', then you are probably going to one day waste a lot of time trying to track down the bug when you write '\\server\share\etc', or be confused when you get syntax errors with 'c:\'.

      So, I strongly suggest you use 'c:\\ggp\\ggp\\ggp' over 'c:\ggp\ggp\ggp' (or use another alternative).

      1 Don't want to overuse that popular "context" word. (:

                      - tye
      Thanks guys, that was a silly mistake *smacking head*
      I appreciate the help!!

      Rohit
Re: inconsistency with ActiveState's open?
by halley (Prior) on Jul 14, 2003 at 15:50 UTC
    Also, unless using system() or `backticks`, Perl on Windows can use forward slashes just as nicely. You could do the following:
    open(FOO, ">c:/gpp/gpp/gpp") or die;

    A few rare APIs in Windows can't deal with it, and the command line user interface usually can't deal with it, but the vast majority of the internal Windows operating system lets you use forward slashes for portability.

    --
    [ e d @ h a l l e y . c c ]