How to pass a hash as optional argument to -M in command line

时间:2015-06-15 15:01:33

标签: perl hash package command-line-interface

I know that when we need to pass some arguments to the use keyword after a package name we can pass them in the command line after the -M parameter.

For example:

use feature 'say';
say 'hello!';

can be invoked from the command line with

>perl -Mfeature=say -e"say 'hello!'"

But what if the argument is a hash? Can I make a one-liner for the following example:

use constant {c1 => 'foo', c2 => 'bar'};
use feature 'say';
say c1, c2; #expected: foobar

This does not work:

>perl -Mfeature=say -Mconstant={c1,'foo',c2,'bar'} -e"say c1,c2"
Constant name '{c1' has invalid characters at -e line 0.
BEGIN failed--compilation aborted.

Neither that:

>perl -Mfeature=say -Mconstant="c1,'foo',c2,'bar'" -e"say c1,c2"
'foo'c2'bar'c2

I know that I can add multiple -Mconstant=foo in the command line, but it is just an example; I have a package here that can take a hash at import and that I am trying to invoke from command-line.

1 个答案:

答案 0 :(得分:1)

Looking at perlrun use:

perl -Mfeature=say "-Mconstant {c1 => 'foo', c2 => 'bar'}" -e"say c1,c2"