Hello!

yesterday I was trying some perl code after neverending boring weeks (workwise..) and I tried to use -s to add rudimentary switch parsing as described here (emphasis added by me):

> -s enables rudimentary switch parsing for switches on the command line after the program name but before any filename arguments (or before an argument of --).

The above is true when there is a real file containing a perl program:

echo print join ' ', $0, $R > switch-test.pl # after the program name perl -s switch-test.pl -R=OK switch-test.pl OK # before an argument of -- perl -s switch-test.pl -R=OK -- switch-test.pl OK # wrong usage perl -s switch-test.pl -- -R=OK switch-test.pl

But what if the program is just some code passed with -e (and this is a valid program also for the program name perl's internal value)?

Even with my limited English understanding, I'd suppose something like: perl -s -e "print $R" R=a or perl -s -e -n "print $R" R=a filename (before any filename arguments) but no, it works exactly in the opposite way:

# after the program name perl -s -e "print join ' ', $0, $R" -R=OK Unrecognized switch: -R=OK (-h will show valid options). # before an argument of -- perl -s -e "print join ' ', $0, $R" -R=OK -- Unrecognized switch: -R=OK (-h will show valid options). # it works in the opposite way of what perlrun states perl -s -e "print join ' ', $0, $R" -- -R=OK -e OK

So, given the switch is stated as rudimentary one should expect to use with small oneliners (only our dead pope used it as signature..) and not for serious programs.

In combination with other switches is even unfriendly:

perl -snwe "print join ' ', $0, $R, $/" -- -R=OK lorem.txt -e OK -e OK -e OK -e OK

..as it shuold be put before any file name but after the --

The docs also warns about warnings:

> Also, when using this option on a script with warnings enabled you may get a lot of spurious "used only once" warnings.

..but no... or.. what happens??

perl -wse "print join ' ', $0, $R" -- -R=OK -e OK perl -swe "print join ' ', $0, $R" -- -R=OK -e OK perl -sew "print join ' ', $0, $R" -- -R=OK #what the hell?! ..no output :)

I think the doc should metion the above. More: in this sight also -- is poorly documented.

Want some monk put a pr somewhere or point me, lazy peon, to the correct repository to strike?

Have a nice weekend!

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re: perlrun doc inaccurate on -s switch
by jo37 (Curate) on Nov 22, 2024 at 10:20 UTC

    In case of -e it's the -e switch that "consumes" the double-dash, signaling "end of perl options". You may use a second double-dash to separate the script's argument from its options.

    $ perl -s -e 'print join ":", $0, $R, @ARGV' -- -R=OK -e:OK $ perl -s -e 'print join ":", $0, $R, @ARGV' -- -- -R=OK -e::-R=OK

    Update

    After re-reading, I dislike my wording. It is not the -e switch consuming the double-dash, it is perl command itself.

    Greetings,
    🐻

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
Re: perlrun doc inaccurate on -s switch
by ikegami (Patriarch) on Nov 23, 2024 at 18:08 UTC

    It's not inaccurate.

    perl -s script.pl -R=val args for program \/ \____/ \______________/ perl -s script opts opts args perl -s script.pl -R=val -- args for program \/ \____/ \______________/ perl -s script opts opts args perl -s -e'...' -- -R=val args for program \________/ \____/ \______________/ perl -s script opts opts args perl -s -e'...' -- -R=val -- args for program \________/ \____/ \______________/ perl -s script opts opts args

    As you can see, the -s options go until an argument that doesn't start with "-" (which they call a "filename argument") or a "--" is reached, and that's the case whether -e is used or not. This is exactly what the paragraph you quoted claims. (Ok, it doesn't say the --" argument is absorbed, but that's not relevant here.)

    What's confusing you is the unmentioned rule that you need a "--" before the options for -s when no file name is provided to perl. perlrun is silent about it, but a "--" is needed before arguments when the first argument starts with "-" (whether -s is used or not). It does mention it accepts "--" (in the synopsis), but it doesn't say what it's for and when it's necessary.

    In the worse case you could have something like this:

    perl {perl opts} -- {opts for -s} -- {script opts} -- {script args}

    Update: Tweaked wording.

      Won't some hero PR this onto the Perl docs, and slightly improve the world!?