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]: $!";
| [reply] [d/l] [select] |
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. | [reply] [d/l] [select] |
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!
| [reply] |
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". ;-)
| [reply] [d/l] |
system(qq{explorer "$url"})
Or if you want to use the user's preferred browser:
system(qq{start "" "$url"})
| [reply] [d/l] [select] |
use Win32::FileOp qw(ShellExecute);
ShellExecute( $url);
better. See Win32::FileOp.
Jenda
Enoch was right!
Enjoy the last years of Rome.
| [reply] [d/l] |
| [reply] [d/l] |