Is it possible to pass command-line arguments to @ARGV when using the -n or -p options?

bgoldst

I think the title of my question basically covers it. Here's a contrived example which tries to filter for input lines that exactly equal a parameterized string, basically a Perlish fgrep -x:

perl -ne 'chomp; print if $_ eq $ARGV[0];' bb <<<$'aa\nbb\ncc';
## Can't open bb: No such file or directory.

The problem of course is that the -n option creates an implicit while (<>) { ... } loop around the code, and the diamond operator gobbles up all command-line arguments for file names. So, although technically the bb argument did get to @ARGV, the whole program fails because the argument was also picked up by the diamond operator. The end result is, it is impossible to pass command-line arguments to the Perl program when using -n.

I suppose what I really want is an option that would create an implicit while (<STDIN>) { ... } loop around the code, so command-line arguments wouldn't be taken for file names, but such a thing does not exist.

I can think of three possible workarounds:

1: BEGIN { ... } block to copy and clear @ARGV.

perl -ne 'BEGIN { our @x = shift(@ARGV); } chomp; print if $_ eq $x[0];' bb <<<$'aa\nbb\ncc';
## bb

2: Manually code the while-loop in the one-liner.

perl -e 'while (<STDIN>) { chomp; print if $_ eq $ARGV[0]; }' bb <<<$'aa\nbb\ncc';
## bb

3: Find another way to pass the arguments, such as environment variables.

PAT=bb perl -ne 'chomp; print if $_ eq $ENV{PAT};' <<<$'aa\nbb\ncc';
## bb

The BEGIN { ... } block solution is undesirable since it constitutes a bit of a jarring context switch in the one-liner, is somewhat verbose, and requires messing with the special variable @ARGV.

I consider the manual while-loop solution to be more of a non-solution, since it forsakes the -n option entirely, and the point is I want to be able to use the -n option with command-line arguments.

The same can be said for the environment variable solution; the point is I want to be able to use command-line arguments with the -n option.

Is there a better way?

mwp

You've basically identified them all. The only one you missed, that I know of at least, is the option of passing switch arguments (instead of positional arguments):

$ perl -sne'chomp; print if $_ eq $kwarg' -- -kwarg=bb <<<$'aa\nbb\ncc';
bb

You could also use one of the many getopt modules instead of -s. This is essentially doing the same thing as manipulating @ARGV in a BEGIN {} block before the main program loop, but doing it for you and making it a little cleaner for a one-liner.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to pass in argv for each parse options using optionparse

分類Dev

How to pass arguments from command line to gradle

分類Dev

Handling command line options with multiple arguments for some flags

分類Dev

how to pass arguments like 750_000 from command line

分類Dev

How can I pass arguments to supervisor from command line

分類Dev

Copy command with command line arguments

分類Dev

Is it possible to pass arguments into a Python script?

分類Dev

is it possible to supply argument when running Rmarkdown in the command line?

分類Dev

What is the command line for Indexing Options?

分類Dev

What is the complete list of streaming command line options possible for Hadoop YARN version?

分類Dev

Command line arguments to Docker CMD

分類Dev

Bash : command line with optional arguments

分類Dev

Is it possible to always automatically pass a command line argument to an install4j installer?

分類Dev

Is it possible to pass an argument in command line for a user to choose to run Selenium headed or headless?

分類Dev

How to give python command line arguments from command line mode

分類Dev

Not getting correct output when using command line argument in java

分類Dev

How to use 'itextESC' when using ex as a text editor in command line?

分類Dev

Bash aliases/functions and command line options

分類Dev

Openshift deploy with additional Maven command line options

分類Dev

Retrieving command line -D options in Java

分類Dev

Display error if no arguments are typed in command line

分類Dev

Parse command line arguments in a Ruby script

分類Dev

How to access command line arguments in Spring configuration?

分類Dev

Command line arguments in swift - convert to interger

分類Dev

Problems while parsing command line arguments

分類Dev

Java command line arguments through shell script

分類Dev

Passing command line arguments through Bash

分類Dev

Visual Studio command line build arguments

分類Dev

getchar to read from command line arguments

Related 関連記事

  1. 1

    How to pass in argv for each parse options using optionparse

  2. 2

    How to pass arguments from command line to gradle

  3. 3

    Handling command line options with multiple arguments for some flags

  4. 4

    how to pass arguments like 750_000 from command line

  5. 5

    How can I pass arguments to supervisor from command line

  6. 6

    Copy command with command line arguments

  7. 7

    Is it possible to pass arguments into a Python script?

  8. 8

    is it possible to supply argument when running Rmarkdown in the command line?

  9. 9

    What is the command line for Indexing Options?

  10. 10

    What is the complete list of streaming command line options possible for Hadoop YARN version?

  11. 11

    Command line arguments to Docker CMD

  12. 12

    Bash : command line with optional arguments

  13. 13

    Is it possible to always automatically pass a command line argument to an install4j installer?

  14. 14

    Is it possible to pass an argument in command line for a user to choose to run Selenium headed or headless?

  15. 15

    How to give python command line arguments from command line mode

  16. 16

    Not getting correct output when using command line argument in java

  17. 17

    How to use 'itextESC' when using ex as a text editor in command line?

  18. 18

    Bash aliases/functions and command line options

  19. 19

    Openshift deploy with additional Maven command line options

  20. 20

    Retrieving command line -D options in Java

  21. 21

    Display error if no arguments are typed in command line

  22. 22

    Parse command line arguments in a Ruby script

  23. 23

    How to access command line arguments in Spring configuration?

  24. 24

    Command line arguments in swift - convert to interger

  25. 25

    Problems while parsing command line arguments

  26. 26

    Java command line arguments through shell script

  27. 27

    Passing command line arguments through Bash

  28. 28

    Visual Studio command line build arguments

  29. 29

    getchar to read from command line arguments

ホットタグ

アーカイブ