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

查看:19
本文介绍了如何在 -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.

  • 参数

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

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

  perl -ne'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 -sne'f($x,$y)' -- -x="$x" -y="$y" -- ...
    

  • 环境变量

  • Environment variables

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

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

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