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

I've a script which is run using the -s switch. I need to compile it and am using perl2exe. The script compiles and runs but the passed -params are ignored.

Anyone help me on this one?

Replies are listed 'Best First'.
Re: perl2exe with -s
by tachyon (Chancellor) on Sep 16, 2004 at 11:56 UTC

    The -s switch is a little problematic on Win32, at least with AS 5.6. perl -s script -VAR=1 works fine but without the -s on the command line (ie with #!perl -s) it does not work. In other words it seems to me that while -w is read off the shebang on Win32 -s is not. Anyway -s implements a very simple mechanism. I would suggest using one of the Getopt modules is a better way to go but you can fake its behaviour with a handful of lines:

    C:\>type test.pl #!/usr/bin/perl use strict; our ( $HELLO, $WORLD ); # fake the behaviour of -s { no strict; my @other = (); while(my $term = shift @ARGV) { $term =~ s/^\-/\$/ ? eval $term : push @other, $term; } @ARGV = @other; } print "Got $HELLO $WORLD\n"; print "\@ARGV = ( @ARGV )\n"; C:\>perl test.pl -HELLO=hello -WORLD=world other arguments Got hello world @ARGV = ( other arguments )

    Note that this code does a trusting string eval which is dangerous but on Win32 your security is already shot ;-). You should probably make it more secure by validating the $term string before the eval but as whoever executes it already has a shell in which to execute arbitrary commands (and the script will exec with their perms) I don't really see it as a major security issue.

    cheers

    tachyon

      There are several ways of avoiding the need to use -s on the command line:

      P:\test>assoc .pl .pl=Perl P:\test>ftype Perl Perl="c:\perl\bin\perl.exe" -sw "%1" %* P:\test>type test.pl #! perl -slw use strict; print 'hello' if our $hello; print 'world' if our $world; P:\test>test -hello -world hello world P:\test>ftype Perl="c:\perl\bin\perl.exe" -w "%1" %* Perl="c:\perl\bin\perl.exe" -w "%1" %* P:\test>set PERL5OPTS=-s P:\test>test -hello -world hello world

      Whether either method would work for the op's problem I have no idea as I've never used perl2exe. Of the two, I would think that setting PERL5OPTS=-s is the more likely to work if either do.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

      Thanks for your help. I've done somthing like this in the past, and I didn't really wont to change the code at this stage but needs must.

      The good people at Indigostar also came up with this
      use Getopt::Long; my ($verbose, $quiet, $test, $debug, $help) = (0,0,0,0,0); GetOptions ('verbose' => \$verbose, 'quiet' => \$quiet, 'test' => \$test, 'help' => \$help, 'debug' => \$debug);

      Thanks again