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

Hello,

On WinXp I'm using the command system("explorer $url") I get problems with an URL that contains an equal sign ("="). It seems as only the part of the URL that is after the equal sign is sent to the explorer which naturally generates an error.

Any suggestions on how to solve this?

Replies are listed 'Best First'.
Re: Problem with strange URLs
by Corion (Patriarch) on Jan 11, 2010 at 16:18 UTC

    You need to properly quote the URL so cmd.exe knows that it's meant to be one string. For example like this:

    my $quoted_url = qq{"$url"}; system("explorer $quoted_url") == 0 or die "Couldn't launch [explorer $quoted_url]: $!";
Re: Problem with strange URLs
by kennethk (Abbot) on Jan 11, 2010 at 16:39 UTC
    To expand upon previous answers, this is really an issue with MS, specifically cmd.exe, rather than Perl. Perl is passing explorer $url, with $url interpolated, to your shell. Lets say $url = http://www.perlmonks.com/?node_id=816742. However, the shell misinterprets the = and attempts to execute the command explorer 816742 which fails to find the server 816742, naturally. The solution is to tell the shell to treat the whole argument as a string by wrapping the url in double quotes. Of course, then you can't use double quotes to wrap the string in Perl, so thank goodness Perl has so many delimiters: Quote and Quote like Operators. Since you require interpolation, qq is a good way to go, a la system(qq{explorer "$url"}), which of course looks eerily similar to ikegami and Corion's solutions above.

    Update: This might actually be explorer messing up the interpolation, since echo http://www.perlmonks.com/?node_id=816742 behaves as expected. Could not find any web resources, however, describing how explorer parses CLI arguments.

      Thank you all for the suggestions! I went with qq and it works like a charm.

      However, I have a new question. Is it possible to somehow get the browser to be on top of my GUI? At the moment the opened browser always ends up behind my GUI window which is quite annoying. Can this be achieved?

      Thanks again!
      This might actually be explorer messing up the interpolation, since echo http://www.perlmonks.com/?node_id=816742 behaves as expected. Could not find any web resources, however, describing how explorer parses CLI arguments.

      Naaaah, that would be too easy. echo is an internal command, and thus gets a different set of parsing rules. Both command.com and cmd.exe have a long history of really smart people finding useful bugs in the parser, and newer versions have to be bug-compatible to allow running old batches. MS explains the various required rules for quoting arguments somewhere in their knowledge base. But compared to a modern unix shell, both command.com and cmd.exe just cause a lot of pain. I avoid them at any cost, usually by using Perl or by switching to Linux.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Problem with strange URLs
by ikegami (Patriarch) on Jan 11, 2010 at 16:18 UTC
    system(qq{explorer "$url"})
    Or if you want to use the user's preferred browser:
    system(qq{start "" "$url"})
Re: Problem with strange URLs
by Jenda (Abbot) on Jan 11, 2010 at 19:31 UTC

    You may like

    use Win32::FileOp qw(ShellExecute); ShellExecute( $url);
    better. See Win32::FileOp.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Re: Problem with strange URLs
by moritz (Cardinal) on Jan 11, 2010 at 16:19 UTC
    Try the list form of system instead: system('explorer', $url);

    Update seems not to work - go with the quoting mentioned in the other replies instead.