in reply to win32 parenthesis system weirdness

Sure you did omit a space or something, cos it works for me:

C:\test>perl -E"system q[dir \"three (3\789655.png\"]" Volume in drive C has no label. Volume Serial Number is 8C78-4B42 Directory of C:\test\three (3 09/09/2009 16:59 14,542 789655.png 1 File(s) 14,542 bytes 0 Dir(s) 411,750,715,392 bytes free

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^2: win32 parenthesis system weirdness
by Anonymous Monk on Dec 04, 2009 at 06:09 UTC
    Nope, I'm not missing any spaces. But your suggestion lead me to a solution, but I'm confused why it works:
    my $fn = '"C:\img\del me\three (3\00000253.jpg"'; system($fn); #Doesn't work my $fn = ' "C:\img\del me\three (3\00000253.jpg"'; system($fn); #Works
    Just by adding a space after the first single quote system() suddenly works. What is the reason for this?

      Shell quoting is a real mess in Windows. It's basically impossible to do right programmatically. And Perl might even make it even worse in some circumstances.

      First, let's see how it doesn't work since you've kept that a secret from us:

      >perl a.pl 'C:\img\del' is not recognized as an internal or external command, operable program or batch file.

      I suspect Perl is executing the following commands:

      >cmd /c "C:\img\del me\three (3\00000253.jpg" 'C:\img\del' is not recognized as an internal or external command, operable program or batch file. >cmd /c " "C:\img\del me\three (3\00000253.jpg"" [cutest bunny ever is displayed]

      (Cutest bunny ever)

      Don't ask my why those commands are those that are run. Don't ask my why that second one even works. Cause I'm not sure I can answer.

      See Re^4: win32 parenthesis system weirdness for a solution.

      Dunno. It works for me with 5.8.9 & 5.10.1. If I omit the 'dir ' from my example, it loads the image into the default image viewer. No leading space required.

      Admittedly I haven't bothered to exactly replicate your weird path. Perhaps that is somehow fooling Perl's command parser into believing that the first part of the path is actually a command and is therefore bypassing the shell and failing.

      If it is a bug, it's probably too obscure to be bothered trying to fix. You have a workaround, though avoiding weird dir names might be easier :)


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Don't think using parenthesis in dirnames is that unusual ;)?! It's not just that single parenthesis at the end that causes this behavior. Paths like this also won't open without that extra space:
        my $fn = q{ C:\img\del me\pic (images)[2k]\00000253.jpg"};
        Tried with perl 5.10 also, but same behaviour:
        #works (note extra space required) "H:\TEST\ActivePerl-5.10.0.1001-MSWin32-x86-283495\perl\bin\perl.exe" +-e "my $fn = ' \"C:\img\del me\three (3\00000253.jpg\"'; system($fn)" #Doesn't work "H:\TEST\ActivePerl-5.10.0.1001-MSWin32-x86-283495\perl\bin\perl.exe" +-e "my $fn = '\"C:\img\del me\three (3\00000253.jpg\"'; system($fn)" 'C:\img\del' is not recognized as an internal or external command, operable program or batch file.
        But since this doesn't work either (as mentioned by ikegami):
        cmd.exe /c "C:\img\del me\three (3\00000253.jpg"
        Then I don't consider this whole problem to be perl related.

        What I now need to understand is why the "(" char is treated in this special way in windows and what other chars create similar weird behaviour? Didn't think "(" was spcecial "meta-char", like | or > etc, that needed to be escaped (and even escaping it with ^ also didn't work).

      ETOOMUCHMAGIC :-)

      If you want to rely on shell behaviour be explicit
      system "cmd.exe /c $fn" system "cmd.exe /c start $fn" system qw' cmd.exe /c start ', $fn;

        None of those work. See Re^3: win32 parenthesis system weirdness for why the first doesn't work.

        As for the last two, it's due to start's very wonky parameter handling. If the first non-option arg is quoted, it's taken to be the title.

        start file.jpg # Works start "file.jpg" # Doesn't work. Same as following start "file.jpg" cmd # Launches cmd in new Window titled file.jpg start "" "file" # Works
        So you want
        my $qfn = 'C:\img\del me\three (3\00000253.jpg'; system(qq{start "" "$qfn"});