I had seen the "-s" option before, when reading perlrun, but had never tried using it till seeing your post. From what I can tell (on macosx 10.4.6, perl 5.8.6), it seems to work only if the script is stored in a file, and has a shebang line with this option flag -- and even then, it doesn't seem to work as advertised, unless the script is executable (runnable without passing it as an arg to "perl"):
$ perl -s -foo=bar -e 'print $foo,$/ if $foo'
Unrecognized switch: -foo=bar (-h will show valid options).
# (and similarly with other arrangements of these args)
$ cat junk.pl
#!/usr/bin/perl -s
print "$foo\n" if $foo;
$ perl junk.pl
$ perl junk.pl -foo
1
$ perl junk.pl -foo=bar
1
# (I thought that should have printed "bar", but
# there must be some logical reason why it didn't.)
$ chmod +x junk.pl
$ junk.pl
$ junk.pl -foo
1
$ junk.pl -foo=bar
bar
# at last!
So -s is just not suitable for use with a one-liner script on the command line. Oh well. At least there are other ways of doing this sort of thing (as indicated by earlier replies).
UPDATE: Turns out the one-liner command-line script approach actually does work if you do it like this:
$ perl -se 'print "$foo\n" if $foo' -- -foo
1
$ perl -se 'print "$foo\n" if $foo' -- -foo=bar
bar
(thanks to blockhead for pointing this out to me.) There was something about the double-dash arg in the perlrun description of "-s", but it didn't seem to apply to this facet of perl's behavior. |