Reputation: 889
If I have this inline command:
perl -pi -e 's/([\da-f]{2})([\da-f]{2})\s?/\\x$1\\x$2\t/g'
Which is simply to substitute four-digit hex, and add it a 'x' in front. -i
used with no filenames on the command line, reading from STDIN
. So for params: 0000 0776
, results are \x00\x00\x07\x76
I know, that if -n
or -p
(with printing) called, perl takes <>
diamond. But I want to pass args only AFTER command, but perl assumes it as files to read. So how do I force -n
or -p
to regard args after command to be regular args for <>
in program, and not args as files to read?
Also, I do not understand the role of i
here. If i would not include it, then I would be adding args line after line (as does <>
), but with i
, it takes all my args at once?
Upvotes: 0
Views: 242
Reputation: 118695
If there are no arguments (i.e., if @ARGV
is empty), then your one-line script (which implicitly uses <>
) will read input from STDIN
. So the solution is to clear @ARGV
at compile time.
perl -pi -e 'BEGIN{@ARGV=()}
s/([\da-f]{2})([\da-f]{2})\s?/\\x$1\\x$2\t/g'
Another solution: Force ARGV
(the implicit file handle that the base <>
operator reads from) to point to STDIN
. This solution doesn't clobber your @ARGV
, if any.
perl -pi -e 'BEGIN{*ARGV=*STDIN}
s/([\da-f]{2})([\da-f]{2})\s?/\\x$1\\x$2\t/g'
Upvotes: 6
Reputation: 9231
The -p option is equivalent to the following code:
LINE:
while (<>) {
... # your program goes here
} continue {
print or die "-p destination: $!\n";
}
-n is the same without the continue block. There's no way to change what it reads from (which is unfortunate, since <<>>
and <STDIN>
are both safer options), but it's pretty easy to replicate it with your modification (the error checking is rarely necessary here):
perl -e 'while (<STDIN>) { s/([\da-f]{2})([\da-f]{2})\s?/\\x$1\\x$2\t/g } continue { print }'
Upvotes: 4