slxt3w
Last Updated: February 25, 2016
·
124
· jr-duboc

Perl: separating invocation arguments from invocation options before using the diamond operator

<> loops through the @ARGV array, so you can mess with @ARGV in any way you want before processing files with <>.
For examples, this script gets rid of any invocation option starting by '-' before processing the input files:

use List::Util qw(first);

foreach $arg (@ARGV) {
     $_ = $arg;
     if (/^-/)
     {
          print "$arg is an invocation option\n";
          #Eliminate invocation options from @ARGV before we run the <> operator
          splice @ARGV, first { @ARGV[$_] eq $arg } 0..$#ARGV, -1;
     }
}

while (<>) {
     chomp;
     print "It was $_ that I saw\n";
}