如何在-n或-p模式下使用Perl处理选项? [英] How can I process options using Perl in -n or -p mode?

查看:79
本文介绍了如何在-n或-p模式下使用Perl处理选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行perl -nperl -p时,每个命令行参数都将作为要打开的文件并逐行处理.如果要将命令行开关传递给该脚本,该怎么办?

When running perl -n or perl -p, each command line argument is taken as a file to be opened and processed line by line. If you want to pass command line switches to that script, how can I do that?

推荐答案

有三种主要方法,可在不使用STDIN或外部存储的情况下将信息传递给Perl.

There are three primary ways of passing information to Perl without using STDIN or external storage.

  • 参数

  • Arguments

使用-n-p时,请提取BEGIN块中的参数.

When using -n or -p, extract the arguments in the BEGIN block.

perl -pe'BEGIN { ($x,$y)=splice(@ARGV,0,2) } f($x,$y)' -- "$x" "$y" ...

  • 命令行选项

  • Command-line options

    在完整程序中,您将使用 Getopt :: Long ,但是 perl -s 在这里就可以了.

    In a full program, you'd use Getopt::Long, but perl -s will do fine here.

    perl -spe'f($x,$y)' -- -x="$x" -y="$y" -- ...
    

  • 环境变量

  • Environment variables

    X="$x" Y="$y" perl -pe'f($ENV{X},$ENV{Y})' -- ...
    

  • 这篇关于如何在-n或-p模式下使用Perl处理选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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