I have a perl script that I call with -p and -f options. I'd like to pass command line parameters to ARGV in the script.
For example, opl.pl
is a script that concatenates each line that doesn't start with xx onto the previous line that starts with xx, with '#' as a separator, after flagging pre-existing '#' characters:
# Usage: perl -pf opl.pl file.txt
BEGIN {$recmark = @ARGV[0] if $#ARGV; }
$recmark = "xx" if (! defined $recmark);
chomp;
print "\n" if /$recmark/;
s/#/\_\_hash\_\_/g;
$_ .= "#"
The script works when no additional parameters are on the command line. E.g., perl -pf oplx.pl filexx.txt
with filexx.txt
:
xx line #1
line 2
line 3
xx line 4
line 5
Produces (aproximately):
xx line __hash__1#line 2#line 3
xx line 4#line 5
I'd like to use perl -pf oplx.pl filexyy.txt yy
with fileyy.txt
:
yy line #1
line 2
line 3
yy line 4
line 5
to produce (aproximately):
yy line __hash__1#line 2#line 3
yy line 4#line 5
Unfortunately, perl parses the command line argument yy
as a file name, rather than as an argument.