3

I want to pass command line options that start with a dash (- or --) to a Perl programm I am running with the -e flag:

$ perl -E 'say @ARGV' -foo
Unrecognized switch: -foo  (-h will show valid options).

Passing arguments that don't start with a - obviously work:

$ perl -E 'say @ARGV' foo
foo

How do I properly escape those so the program reads them correctly?

I tried a bunch of variations like \-foo, \\-foo, '-foo', '\-foo', '\\-foo'. None of those work though some produce different messages. \\-foo actually runs and outputs \-foo.

2 Answers 2

6

You can use the -s, like:

perl -se 'print "got $some\n"' -- -some=SOME

the above prints:

got SOME

From the perlrun:

-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 --). Any switch found there is removed from @ARGV and sets the corresponding variable in the Perl program. The following program prints "1" if the program is invoked with a -xyz switch, and "abc" if it is invoked with -xyz=abc.

            #!/usr/bin/perl -s
            if ($xyz) { print "$xyz\n" }

        Do note that a switch like --help creates the variable "${-help}", which is not compliant with "use strict
        "refs"".  Also, when using this option on a script with warnings enabled you may get a lot of spurious
        "used only once" warnings.

For the simple arg-passing use the --, like:

perl -E 'say "@ARGV"' -- -some -xxx -ddd

prints

-some -xxx -ddd
Sign up to request clarification or add additional context in comments.

2 Comments

Actually I only want the --. Thanks for that. :)
I don't need the parsing, I just need them to go through to the program.
3

Just pass -- before the flags that are to go to the program, like so:

  perl -e 'print join("/", @ARGV)' -- -foo bar

prints

  -foo/bar

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.