How can I process options using Perl in -n or -p mode?

后端 未结 2 1027
我寻月下人不归
我寻月下人不归 2020-11-30 12:24

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

2条回答
  •  再見小時候
    2020-11-30 12:25

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

    • Arguments

      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

      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})' -- ...
      

提交回复
热议问题