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

Why doesn't this work?
my $fn = '"C:\img\del me\three (3\00000253.jpg"'; system($fn);
when this works fine:
my $fn = '"C:\img\del me\three 3\00000253.jpg"'; system($fn)
Somehow the "(" char causes some problem. Why?

MSWin32|perl5.8.8 (ActiveState)

Replies are listed 'Best First'.
Re: win32 parenthesis system weirdness
by BrowserUk (Patriarch) on Dec 03, 2009 at 23:15 UTC

    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.
      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.

        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;
Re: win32 parenthesis system weirdness
by GrandFather (Saint) on Dec 04, 2009 at 00:04 UTC

    Try:

    my $fn = 'C:\img\del "me\three (3\00000253.jpg"'; system($fn);

    Note the change in location of the first ".

    Update: thought for another second and realised I was fooled by the path!


    True laziness is hard work