in reply to Stupid If Question

First, EVEN ON WINDOWS:
#!/usr/bin/perl -w use strict;
Windows does NOT care about the path to Perl, BUT it does care about the -w (use warnings). This standard header will work on both Unix and Windows.

Second, This "back-slash" versus "forward-slash" stuff can be a mess! The ANSWER is that Perl has fixed this! Always use Unix "/" for all path names known to Perl. $root = "C:/xyzzy", NOT "C:\\xyzzy"! Perl will translate the "/" to back slash when needed, which relieves you of the problem of having to code "\\path" vs "/path". This "escape char" stuff is a HUGE source of errors and its not needed.

Third, using characters that could have meaning within a print statement (like < or >) can potentially get you into trouble. This is the time for "\", or just using commas to separate tokens:

print "zip_exe = "<",$zipexe,">\n"; #"<$zipexe>$x" might not parse "right" in the #general case although usage in above code appears ok..
I've seen code before that uses the "." concatenation op instead of just a simple comma. In Perl this is just not necessary. Use "," when you can do so.

Replies are listed 'Best First'.
Re^2: Stupid If Question
by ikegami (Patriarch) on Aug 19, 2009 at 03:36 UTC

    Perl will translate the "/" to back slash when needed

    I doubt it since the OS happily accepts "/" as a path separator.

    While the following isn't proof, it would surprise most:

    C:\Documents and Settings\ikegami>dir "c:/bin" Volume in drive C is C Volume Serial Number is 28AB-0E8B Directory of c:\bin 2008/12/21 10:53 PM <DIR> . 2008/12/21 10:53 PM <DIR> .. . . .
      There are differences in MS OS, consider:

      Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\Projects>cd /temp The system cannot find the path specified. C:\Projects>cd \temp C:\TEMP>
      There can be weird things with "\". I am just saying for the most part, use "/" instead of "\\". I think we agree on that!

        There are differences in MS OS, consider:

        uh no. We're using the same OS. It seems you didn't execute the same command as I did.

        You need to use quotes since a bare "/" denotes an switch. And while some shell commands and utilities accept "/", chdir aka cd isn't one of them.

        Going back to what I said, the OS (WinXP) itself accepts "/" throughout its API. This is how Perl communicates with the OS, so it's free to use "/".

        There can be weird things with "\". I am just saying for the most part, use "/" instead of "\\". I think we agree on that!

        I made a comment on the text I quoted specifically. The advice you give is sound, although I use "\" personally. "/" just looks to weird.

Re^2: Stupid If Question
by Anonymous Monk on Aug 19, 2009 at 07:05 UTC
    Not even on windows :)
    #!/usr/bin/perl -- use strict; use warnings;
    Get with the times, -w is dead, long live warnings :D
      Wrong.

      -w shebang option is alive and well at least in Perl 5.10. And yes this is supported on Windows (not the path), but the -w option just like I said.

      Update: For folks that didn't read the previous:

      #!/usr/bin/perl -w use strict;
      should be almost like a mantra on Unix or on Windows!