xargs 将多个参数传递给 perl 子例程? [英] xargs pass multiple arguments to perl subroutine?

查看:22
本文介绍了xargs 将多个参数传递给 perl 子例程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用 xargs 传输多个参数:

I know how to pipe multiple arguments with xargs:

echo a b | xargs -l bash -c '1:$0 2:$1'

而且我知道如何将参数数组从 xargs 传递给我的 perl 模块的子例程:

and I know how to pass the array of arguments to my perl module's subroutine from xargs:

echo a b | xargs --replace={} perl -I/home/me/module.pm -Mme -e 'me::someSub("{}")'

但我似乎无法使用这些美元引用将多个个人参数传递给 perl(以满足 me::someSub 签名):

But I can't seem to get multiple individual arguments passed to perl using those dollar references (to satisfy the me::someSub signature):

echo a b | xargs -l perl -e 'print("$0 $1")'

只需打印:

-e

那么我如何获得 shell 参数:$0, $1 传递给我的 perl 模块的子程序?

So how do I get the shell arguments: $0, $1 passed to my perl module's subroutine?

我知道我可以只分隔 a;b 以便 xarg {} 可以通过 perl 拆分它以获得单个参数来处理),但我也可以用 perl 完全处理所有 STDIN.相反,我的目标是使用 perl -e 以便我可以显式调用我想要的子例程(而不是在脚本中进行一些预处理来确定要调用的子例程和要使用的参数基于标准输入,避免脚本维护成本).

I know I could just delimit a;b so that the xarg {} could be processed by perl splitting it to get individual arguments), but I could also just completely process all STDIN with perl. Instead, my objective is to use perl -e so that I can explicitly call the subroutine I want (rather than having some pre-process in the script that figures out what subroutine to call and what arguments to use based on STDIN, to avoid script maintenance costs).

推荐答案

我不确定你的设计细节,所以我认为你需要一个 Perl one-liner 来使用在作用域中看到的 shell 的变量在其中调用.

I am not sure about the details of your design, so I take it that you need a Perl one-liner to use shell's variables that are seen in the scope in which it's called.

A perl -e'...' 执行在 '' 下给出的 Perl 程序.对于来自该程序运行环境的任何变量——管道或 shell 脚本——要可供程序使用,它们的值需要传递给它.这篇博文中详细说明了使用单线完成此操作的方法,这里是摘要.

A perl -e'...' executes a Perl program given under ''. For any variables from the environment where this program runs -- a pipeline, or a shell script -- to be available to the program their values need be passed to it. Ways to do this with a one-liner are spelled out in this post, and here is a summary.

Perl 程序接收在命令行上以 @ARGV 数组传递给它的参数.所以你可以在管道中调用它作为

A Perl program receives arguments passed to it on the command-line in @ARGV array. So you can invoke it in a pipeline as

... | perl -e'($v1, $v2) = @ARGV; ...' "$0" "$1"

或作为

... | xargs -l perl -e'($v1, $v2) = @ARGV; ...'

if xargs 确实用于向 Perl 程序提供其输入.在第一个示例中,引用变量是为了保护其中可能的有趣字符(空格、* 等)不被设置和运行 perl 程序的 shell 解释.

if xargs is indeed used to feed the Perl program its input. In the first example the variables are quoted to protect possible interesting characters in them (spaces, *, etc) from being interpreted by the shell that sets up and runs the perl program.

如果输入包含要处理的多行,并且单行使用 -n-p 作为它,则在 BEGIN 中解包参数块

If input contains multiple lines to process and the one-liner uses -n or -p for it then unpack arguments in a BEGIN block

... | perl -ne'BEGIN { ($v1, $v2) = splice(@ARGV,0,2) }; ...'  "$0" "$1" ...

在编译时运行,所以在 -n/-p 提供的输入行上循环之前.现在从 @ARGV 中删除了文件名以外的参数,因此只为 -n/-p 保留文件名,以防输入来自文件.

which runs at compile time, so before the loop over input lines provided by -n/-p. The arguments other than filenames are now removed from @ARGV, so to leave only the filenames there for -n/-p, in case input comes from files.

在单行模式中,还有一种基本的命令行开关机制,通过 -s 开关.详情请参阅上面的链接;我会推荐 @ARGV 而不是这个.

There is also a rudimentary mechanism for command-line switches in a one-liner, via the -s switch. Please see the link above for details; I'd recommend @ARGV over this.

最后,您的调用代码可以设置环境变量,然后在%ENV 中的 Perl 程序可以使用这些变量.但是,这似乎并不适合您想要的.

Finally, your calling code could set up environment variables which are then available to the Perl progam in %ENV. However, that doesn't seem to be suitable to what you seem to want.

另请参见这篇文章.

这篇关于xargs 将多个参数传递给 perl 子例程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆