如何将变量从PowerShell传递到Perl脚本?

发布于 2025-02-10 04:22:56 字数 237 浏览 2 评论 0原文

如果我将以下保存为 new.pl ,我如何将$ name从powershell命令行传递给它,以便我可以运行并从ps设置变量:

print "Hello, world, from $name.\n";

i尝试使用start-process并使用cmd.exe /c < /code>,但真的不知道需要什么。

If I have the below saved as new.pl, how do I pass $name from the powershell command line to it so I can run and set the variable from PS:

print "Hello, world, from $name.\n";

I tried start-process and using cmd.exe /C but don't really know what is needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹉夏雨初晴づ 2025-02-17 04:22:56

您只需使用

perl a.pl $name

@ARGV中提供值。

use v5.20;
use warnings;

my ( $name ) = @ARGV;

say "Hello, world, from $name.";
PS> $name = "your friendly neighbourhood Spiderman"
PS> perl a.pl $name
Hello, world, from your friendly neighbourhood Spiderman.

作为单线:

perl "-Mv5.20" -e"say qq{Hello, world, from `$ARGV[0].}" $name

如果您需要在使用-n-p时进行参数,请参见我如何使用perl在-n或-p模式下处理选项?

You'd simply use

perl a.pl $name

This provides the value in @ARGV.

use v5.20;
use warnings;

my ( $name ) = @ARGV;

say "Hello, world, from $name.";
PS> $name = "your friendly neighbourhood Spiderman"
PS> perl a.pl $name
Hello, world, from your friendly neighbourhood Spiderman.

As a one-liner:

perl "-Mv5.20" -e"say qq{Hello, world, from `$ARGV[0].}" $name

If you need to pass arguments while using -n or -p, see How can I process options using Perl in -n or -p mode?

迷你仙 2025-02-17 04:22:56

我找到 this 答案说您可以使用-s命令行开关以便能够传递任意数据。此答案中提供的示例是:

perl -se 'print "xyz=$xyz\n"' -- -xyz="abc"

-e在此示例中仅允许从命令行执行代码,在这种情况下,这不是必需的,因为您正在运行文件,所以我们可以摆脱它。我对Perl没有经验,也找不到有关-做什么的任何内容。如果有人知道随时发表评论,我会将其添加到答案中,但是现在我只是忽略它,因为它似乎没有它。

经过这些修改后,上面的命令看起来像这样:

perl -s new.pl -name="joe"

很简单。现在,我们可以很容易地将其转换为PowerShell命令。 Perl是程序正在运行,其他所有内容都是参数。使用start-process看起来像这样:

Start-Process perl -ArgumentList "-s new.pl -name=`"joe`""

现在确实有效,但是默认情况下,start-process创建了一个新窗口,因此它正在打开Perl Instraper,该窗口立即关闭了因为没有什么可以告诉它等待的。如果您希望它将输出打印到PowerShell窗口,则只需使用-nonewwindow switch即可。

Start-Process perl -ArgumentList "-s new.pl -name=`"joe`"" -NoNewWindow

I found this answer which says you can use the -s command line switch in order to be able to pass arbitrary data. The example provided in this answer is this:

perl -se 'print "xyz=$xyz\n"' -- -xyz="abc"

The -e switch in this example just allows execution of code from the command line, which isn't necessary in this case because you're running a file, so we can just get rid of it. I'm not experienced with perl and can't find anything on what the -- does. If someone knows feel free to leave a comment and I will add it to the answer, but for now I'll just ignore it because it seems to work without it.

After these modifications, the command above would look something like this:

perl -s new.pl -name="joe"

Pretty simple. Now we can very easily convert this to a PowerShell command. perl is the program being ran, and everything else is arguments. using Start-Process it would look like this:

Start-Process perl -ArgumentList "-s new.pl -name=`"joe`""

Now this does work, but by default Start-Process creates a new window, so it's opening the perl interpreter which just instantly closes because there's nothing telling it to wait. If you want it to print the output to the PowerShell window, you can just use the -NoNewWindow switch.

Start-Process perl -ArgumentList "-s new.pl -name=`"joe`"" -NoNewWindow
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文