如何将变量从PowerShell传递到Perl脚本?
如果我将以下保存为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需使用
@ARGV
中提供值。作为单线:
如果您需要在使用
-n
或-p
时进行参数,请参见我如何使用perl在-n或-p模式下处理选项?You'd simply use
This provides the value in
@ARGV
.As a one-liner:
If you need to pass arguments while using
-n
or-p
, see How can I process options using Perl in -n or -p mode?我找到 this 答案说您可以使用
-s
命令行开关以便能够传递任意数据。此答案中提供的示例是:-e
在此示例中仅允许从命令行执行代码,在这种情况下,这不是必需的,因为您正在运行文件,所以我们可以摆脱它。我对Perl没有经验,也找不到有关-
做什么的任何内容。如果有人知道随时发表评论,我会将其添加到答案中,但是现在我只是忽略它,因为它似乎没有它。经过这些修改后,上面的命令看起来像这样:
很简单。现在,我们可以很容易地将其转换为PowerShell命令。
Perl
是程序正在运行,其他所有内容都是参数。使用start-process
看起来像这样:现在确实有效,但是默认情况下,
start-process
创建了一个新窗口,因此它正在打开Perl Instraper,该窗口立即关闭了因为没有什么可以告诉它等待的。如果您希望它将输出打印到PowerShell窗口,则只需使用-nonewwindow
switch即可。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: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:
Pretty simple. Now we can very easily convert this to a PowerShell command.
perl
is the program being ran, and everything else is arguments. usingStart-Process
it would look like this: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.