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

Situation:
  * Win2K
  * ActiveState Perl

I'm trying to write a very simple script that will generate a URL with form information and open up Internet Explorer with that URL. (the script is below)

Everything works, except that the "&skip=5" does not show up in internet explorer no matter what I try. Putting it in quotes, single quotes, preceding with a backslash, etc. There is some kind of problem with windows/perl/& that I don't get.

here is the script (or one version of it) - if you want to test it, you should go to the website first and get the appropriate cookies setup for your location.

---------------------------------------------------- $date = sprintf("%04d%02d%0d", (localtime)[5]+1900, (localtime)[4]+1, (localtime)[3] ); $url = "www.moviefone.com/showtimes/closesttheaters.adp?date=$date\&sk +ip=5"; $command = "\"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE\" $ur +l"; print $command; exec ($command); ----------------------------------------------------

Edit by tye to preserve formatting

Replies are listed 'Best First'.
(tye)Re: windows command line problem?
by tye (Sage) on Oct 16, 2002 at 17:12 UTC

    The usual advice would be to use

    exec( "c:/program files/Internet explorer/IEXPLORE.EXE", $url, );
    to avoid command-line shell misinterpretation risks. Unfortunately, that trick doesn't work in Win32 and Perl doesn't (yet -- I still have hope) make it even "almost work".

    I'd go with: $command = qq("C:/Program Files/Internet Explorer/IEXPLORE.E­XE" "$url"); because you need quotes around the URL because & has meaning to the shell just like the spaces do (which is what forced you to put quotes around the path to IE).

            - tye (but the shell thinks I'm Tye)
      I tried your qq... method - in fact, I copied and pasted to make sure I didn't have a typo, and for reasons I have yet to understand, it failed to open internet explorer. I put a "sleep 10" before the exec, so I could make sure the command looked good, which it did to me...

        Sorry, the code inside of perl.exe that gets run is convoluted (it converts the arguments to exec into a string which it then converts into a list of command words, which are then converted back into a string, *sigh*) and rather buggy. It has also changed between versions of Perl so exactly what works depends on what version of Perl you have.

        You could use Win32::Process which gives you much more direct access to what exec ends up calling anyway.

        But, you can also just avoid quotes around the executable name which appears to be what triggers several of the bugs. Luckily, these are easy to avoid by using Win32::GetShortPathName():

        use strict; use Win32; my $date = sprintf("%04d%02d%0d", (localtime)[5]+1900, (localtime)[4]+1, (localtime)[3] ); my $url = "www.moviefone.com/showtimes/closesttheaters.adp?date=$date\ +&skip=5"; my $command = Win32::GetShortPathName( "C:/Program Files/Internet Explorer/IEXPLORE.EXE" ); $command = qq($command "$url"); print "$command"; ''.<STDIN>; exec( $command );
        which I tested. (:

                - tye ('Tye' is not recognized as an internal or external command)
Re: windows command line problem?
by BrowserUk (Patriarch) on Oct 16, 2002 at 17:07 UTC

    There are many better ways of doing what you are doing, but the specific problem with your code is that '&' has a special meaning on Windows command line. The simplest way to get your snippet to work would be to substitute the alternate url parameter separator ';' for the '&', I just tried this and it worked fine with NT4/IE 5.5.

    Update: A third alternative (to the one above and quoting the whole thing as zigdon said) is to escape the '&' with the CMD meta quote character '^'. Eg.

    "...?date=$date^&skip=5";


    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
      The ^ method worked. Thanks.
Re: windows command line problem?
by zigdon (Deacon) on Oct 16, 2002 at 17:02 UTC
    Try surrounding the $url in "" - the & character is special in dos, and has to be quoted.

    -- Dan

Re: windows command line problem?
by Mr. Muskrat (Canon) on Oct 16, 2002 at 22:28 UTC

    See this thread (Fetch a cookie to disable X10 popup ads) for a demo of using Win32::OLE to do it.

    Update: I'd use Win32::OLE to do it!

    #!/usr/bin/perl use strict; use warnings; use Win32::OLE; # your date creation code my $date = sprintf("%04d%02d%0d", (localtime)[5]+1900, (localtime)[4]+1, (localtime)[3] ); # URL to get my $url = "http://www.moviefone.com/showtimes/closesttheaters.adp?date +=$date&skip=5"; # Start IE my $ie = Win32::OLE->new('InternetExplorer.Application') or die "Error getting IE instance: " . Win32::OLE->LastError(); # Show me IE, otherwise I won't know if it worked $ie->{Visible} = 1; # Navigate to the URL $ie->Navigate($url); # sleep until the page is loaded do { sleep(1); } until ($ie->{ReadyState} == 4); # do something useful here # kill IE when done $ie->quit;