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

Greetings

Can someone tell me why this doesn't DWIM?
$ perl -se 'print $foo' -foo=bar Unrecognized switch: -foo=bar (-h will show valid options).
I've tried other configurations:
perl -e -s 'print $foo' -foo=bar
doesn't do anything
$ perl -es 'print $foo' -foo=bar Substitution pattern not terminated at -e line 1. $ perl -e -s -foo=bar 'print $foo' Unrecognized switch: -foo=bar (-h will show valid options).
I know it's not a quoting issue:
$ perl -e 'print $=' 60
Any thoughts?

Replies are listed 'Best First'.
Re: -s switch with -e
by lidden (Curate) on Apr 17, 2006 at 20:43 UTC
    perl -se 'print $foo' -- -foo=bar

    Works, without '--' the switch is sent to perl instead of your script.
Re: -s switch with -e
by ikegami (Patriarch) on Apr 17, 2006 at 22:27 UTC

    Since noone explained the solution, I will:

    Perl will absorb everything that starts with a dash until it meets something that doesn't start with dash or until it meets "--". The code attached to -e is considered part of -e, so it doesn't interrupt the scanning for options.

    -s scans whatever is left would otherwise go to @ARGV.

    Interpreted as perl options vv vvvvvvvvvv vvvvvv perl -s -e "print" -foo=a a b ^ ^ @ARGV, which -s scans Interpreted as perl options vv perl -s script.pl -foo=a a b ^^^^^^ ^ ^ @ARGV, which -s scans Interpreted as perl options vv vvvvvvvvvv vv perl -s -e "print" -- -foo=a a b ^^^^^^ ^ ^ @ARGV, which -s scans
Re: -s switch with -e
by Tanktalus (Canon) on Apr 17, 2006 at 20:44 UTC

    I can suggest trying:

    $ perl -se 'print $foo,$/' -- -foo=bar bar
    I kind of extrapolated this from the perlrun docs, and, although I kinda can see it (perl trying to figure out where its arguements stop and yours start), it definitely doesn't do what I expect based on it being able to figure this out just fine when not using the -s flag.

Re: -s switch with -e
by tempest (Sexton) on Apr 17, 2006 at 20:48 UTC
    this is meant for program files. why not just
    perl -e '$foo="bar";print $foo'
    or just save the file as

    #!/usr/bin/perl -s print $foo; #then in the command line #file.pl -foo=bar
      sorry... the impetus behind the question was this node. I was trying to wrap my mind around a solution I thought should be pretty slick but that I couldn't make work in practice.

      Thanks to everyone who helped me get my mind around this.
      Apologies to graff who's update I didn't consult in time.