How can I get a Perl script to accept parameters from both STDIN and command line arguments?

perl

Solution

This is rather easy to do using perl's diamond operator

push(@ARGV, "/dev/stdin");
while(<>) {
  print;
}

For example:

$ echo A > a
$ echo B > b
$ echo C | perl test.pl a b
A
B
C

Unfortunately this does rely on `/dev/stdin`, but is still fairly portable.

Problem

Inspired by this U&L Q&A titled: "https://unix.stackexchange.com/questions/171150/back-to-back-pipes-into-a-command". How could one parse both input via STDIN and via command line arguments to a Perl script? For example I'd like a script that can consume input parameters from both STDIN and via command line arguments: ``` $ command | my_command.pl arg1 arg2 ``` And the output of `command` would be ``` arg3 arg4 ... ``` So while `my_command.pl` is running, it would be aware of the parameters arg1, arg2, arg3, and arg4.

Original source