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

Perl Version = 5.8.1 (ActiveState)
Running on Win2000 SP3

Here's my conundrum..

I have a very basic script that accepts parameters and prints them to the screen (dumbed down version of real issue). When I run the script ie C:\foo.pl foo bar

I get nothing.

When I explicitly call perl first, ie C:\perl foo.pl foo bar

It works just fine.

We uninstalled and reinstalled perl, and we have verified that the .pl extention is registered to use the "Perl Command Line Interpreter" (perl.exe)

Any ideas?

Also curious if the #!C:\Perl\bin\perl -w is of any value in the Win32 environment?

Replies are listed 'Best First'.
Re: perl script on Win32
by gellyfish (Monsignor) on Aug 06, 2004 at 13:58 UTC

    I'm afraid that this is a problem with windows and not just with Perl - when you have an association with an extension like that it doesn't properly pass on the command line arguments. If you want to run the Perl program at the command line like that without specifiying the perl interpreter then you might want to look at using the pl2bat program that comes with ActivePerl. This will wrap your program as a windows .bat file that can be run and will pass on the command line arguments.

    /J\

Re: perl script on Win32 (stale env)
by tye (Sage) on Aug 06, 2004 at 15:22 UTC

    When you say "I get nothing", I think you really meant to say that you get an error message and to actually tell us exactly what that error message was (cut'n'paste it).

    I'd guess that your PATH got updated but you were trying to run the Perl script in a command window that didn't have the up-to-date PATH. See CleanPath for once way to make dealing with this a bit easier.

    The trailing \ that you noticed wouldn't have been a problem (as you verified). You most likely used the GUI to change the environment settings and this notifies your desktop 'shell' (explorer.exe, not the command 'shell' that Unix users are used to thinking of when they hear 'shell') to fetch the latest environment settings. If you then open a new command prompt, it will have the updated PATH (so that perl.exe can be found).

    And, of course, the claims in this thread that you can't pass arguments via file associations in Windows are wrong (as you verified).

    See what you get when you run these commands:

    C:\>assoc .pl .pl=Perl C:\>ftype Perl Perl=perl -Mouse "%1" %* C:\>

    My output shows that I've set my associations to use my -Mouse module so that double-clicking a *.pl file (or drag'n'dropping to a *.pl file or downloading a *.pl file from a trusted web site [risky!]) will not create a command window that just disappears when the script finishes or fails.

    The %* is what causes the arguments to get passed along. The "%1" is the name of the script and the quotes are what allow it to work on paths/files that contain spaces.

    You might also like to try pl2bat. Although the bug of "script.pl >output.txt" and "script.pl | more" not working with file associations has been fixed in modern versions of Windows, I still use pl2bat for all of my Perl scripts (in part so that my -Mouse trick doesn't affect scripts meant to be run from the command line).

    - tye        

Re: perl script on Win32
by dsb (Chaplain) on Aug 06, 2004 at 13:51 UTC
    You have to edit your PATH to include C:\Perl\bin.


    dsb
    This @ISA my cool %SIG
      THANK YOU very much.

      We found that our path had C:\Perl\bin\ (with a trailing slash). We updated the path environment variable to be C:\Perl\bin without the path and it started to work.

      HOWEVER, being the curious person I am, I switched my path environment variable back to C:\Perl\bin\, lo and behold my script still worked. I was expecting it to fail again.

      Any thoughts on this one, or should I just leave well enough alone?
Re: perl script on Win32
by mscerra (Monk) on Aug 06, 2004 at 13:57 UTC
    Like DSB said you need to add perl to your path to run it from the command line.

    In answer to your final question Activestate perl reads #! C:\Perl\bin\perl -w and gets the -w flag so it will run the program with warnings. It will not use that line to find where perl resides.

      Windows doesn't care about the shebang, so I use #!/cygdrive/c/perl/bin/perl in my scripts. That way they still use ActiveState when I run them under cygwin. (because DBD::Oracle doesn't work (for me) under cygwin)
Re: perl script on Win32
by elwarren (Priest) on Aug 06, 2004 at 14:47 UTC
    You can do:
    use warnings; use strict;
    instead of the -w flag.