如何在命令行中将哈希作为可选参数传递给 -M
How to pass a hash as optional argument to -M in command line
我知道当我们需要将一些参数传递给包名后的 use
关键字时,我们可以在命令行中的 -M
参数后传递它们。
例如:
use feature 'say';
say 'hello!';
可以通过
从命令行调用
>perl -Mfeature=say -e"say 'hello!'"
但是如果参数是散列呢?我可以为以下示例制作单行吗:
use constant {c1 => 'foo', c2 => 'bar'};
use feature 'say';
say c1, c2; #expected: foobar
这不起作用:
>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.
两者都不是:
>perl -Mfeature=say -Mconstant="c1,'foo',c2,'bar'" -e"say c1,c2"
'foo'c2'bar'c2
我知道我可以在命令行中添加多个-Mconstant=foo
,但这只是一个例子;我这里有一个包,可以在导入时使用哈希,我正试图从命令行调用它。
查看 perlrun 使用:
perl -Mfeature=say "-Mconstant {c1 => 'foo', c2 => 'bar'}" -e"say c1,c2"
我知道当我们需要将一些参数传递给包名后的 use
关键字时,我们可以在命令行中的 -M
参数后传递它们。
例如:
use feature 'say';
say 'hello!';
可以通过
从命令行调用>perl -Mfeature=say -e"say 'hello!'"
但是如果参数是散列呢?我可以为以下示例制作单行吗:
use constant {c1 => 'foo', c2 => 'bar'};
use feature 'say';
say c1, c2; #expected: foobar
这不起作用:
>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.
两者都不是:
>perl -Mfeature=say -Mconstant="c1,'foo',c2,'bar'" -e"say c1,c2"
'foo'c2'bar'c2
我知道我可以在命令行中添加多个-Mconstant=foo
,但这只是一个例子;我这里有一个包,可以在导入时使用哈希,我正试图从命令行调用它。
查看 perlrun 使用:
perl -Mfeature=say "-Mconstant {c1 => 'foo', c2 => 'bar'}" -e"say c1,c2"