Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...