Magic Argv
This page shows how to rewrite@ARGV so Perl transparently decompresses .gz, .bz2, and .xz files while you run a single perl -e command. It is a sysadmin shortcut for grepping rotated logs without writing a wrapper script.
Here is the scenario. You manage a server where logs rotate nightly and compress automatically. Your directory looks like this:
Some files are plain text and some are compressed. You need to grep through all of them.access.log access.log.1 access.log.2.gz access.log.3.gz access.log.4.gz
The ugly way looks like this:
That requires two commands, a wrapper script, or a shell loop. The one-liner below handles both in a single pass:cat access.log access.log.1 zcat access.log.*.gz
One command handles both formats. The diamond operator sees pipes and plain files and treats them the same way.perl -e ' s/^([^'"'"']+\.gz)\z/gzcat '"'"'$1'"'"' |/ for @ARGV; print while <>; ' access.log*
Part 1: THE TRICK
Before the diamond operator runs, we rewrite @ARGV:Files ending ins/^([^']+\.gz)\z/gzcat '$1' |/ for @ARGV;
.gz become:
That trailing pipe tells Perl to run the command and read its output. The diamond operator sees the pipe, executesaccess.log.2.gz -> gzcat 'access.log.2.gz' |
gzcat, and hands you decompressed data.
Plain files pass through unchanged. The regex simply does not match them.
Part 2: HOW PIPES IN @ARGV WORK
The diamond operator has a few magic behaviors:A trailing pipe means read from that command. A leading pipe means write to that command.ARGV ENTRY PERL DOES ---------------- ---------------------------------- filename open(ARGV, '<', 'filename') command | open(ARGV, '-|', 'command') | command open(ARGV, '|-', 'command')
This is old-school two-argument open behavior. It is usually dangerous. We are exploiting it deliberately here.
.--. |o_o | |:_/ | // \ \ (| | ) /'\_ _/`\ \___)=(___/
Part 3: THE REGEX EXPLAINED
Why uses/^([^']+\.gz)\z/gzcat '$1' |/ PIECE MEANING -------- ------------------------------------------ ^ Start of string ( Begin capture [^']+ One or more chars that aren't single quotes \.gz Literal .gz ) End capture \z End of string gzcat '$1' Replacement: gzcat command with filename | Trailing pipe - read from command
[^']+ instead of .+?
Security. If someone passes a filename containing a single quote, they could inject commands into your shell. The [^'] character class prevents that.
evil'file;rm -rf /.gz # Won't match - has quote
Part 4: MULTIPLE COMPRESSION FORMATS
Handle.gz and .Z files:
Handles/^([^']+\.(?:gz|Z))\z/gzcat '$1' |/ for @ARGV;
.bz2 too:
Two substitutions handle three formats.s/^([^']+\.bz2)\z/bzcat '$1' |/ for @ARGV; s/^([^']+\.(?:gz|Z))\z/gzcat '$1' |/ for @ARGV;
Or combine them:
for (@ARGV) { s/^([^']+\.(?:gz|Z))\z/gzcat '$1' |/; s/^([^']+\.bz2)\z/bzcat '$1' |/; s/^([^']+\.xz)\z/xzcat '$1' |/; }
Part 5: PRACTICAL SCRIPT
Make it a reusable tool:Now you can run:#!/usr/bin/env perl # zperl - run perl with transparent decompression for (@ARGV) { next if /^\s*-/; # Skip flags s/^([^']+\.(?:gz|Z))\z/gzcat '$1' |/; s/^([^']+\.bz2)\z/bzcat '$1' |/; s/^([^']+\.xz)\z/xzcat '$1' |/; s/^([^']+\.zst)\z/zstdcat '$1' |/; } # Hand off to regular perl with modified @ARGV exec 'perl', @ARGV;
That handles all compression formats transparently.zperl -ne 'print if /error/' access.log*
Part 6: GREP ALL LOGS
Search through rotated logs:The $ARGV variable tells you which file or pipe you are reading from.perl -e ' s/([^'"'"']+\.gz)$/gzcat '"'"'$1'"'"' |/ for @ARGV; print "$ARGV: $_" while <>; ' /var/log/syslog* | grep -i error
Or write it in pure Perl:
perl -e ' s/([^'"'"']+\.gz)$/gzcat '"'"'$1'"'"' |/ for @ARGV; print "$ARGV: $_" if /error/i while <>; ' /var/log/syslog*
Part 7: THE QUOTE NIGHTMARE
Those ugly quote escapes look like this:That is shell escaping. You cannot nest single quotes inside a single-quoted string. So you close the quote, insert an escaped single quote, and reopen it:'"'"'
The result1. End single quote: ' 2. Add escaped single quote: "'" 3. Resume single quote: '
'"'"' produces a literal single quote in the final string.
Inside a script file, the escaping disappears:
No shell escaping is needed.s/^([^']+\.gz)\z/gzcat '$1' |/ for @ARGV;
Part 8: SECURITY NOTES
This trick relies on two-argument open semantics. That pattern is usually bad practice.The [^']+ character class in the regex protects against quote injection. But other special characters still matter:
If someone controls your filenames, they control your system.>file # Opens for writing |command # Pipe to command command| # Pipe from command
A safer version looks like this:
Thefor (@ARGV) { if (/\.gz$/ && -f $_) { # Must be a real file $_ = "gzcat \Q$_\E |"; # \Q quotes metacharacters } }
-f check confirms it is a regular file. The \Q...\E escapes any shell metacharacters.
Part 9: MODERN ALTERNative
PerlIO::gzip does this cleanly:That requires installing a module. The @ARGV trick works with stock Perl.perl -MPerlIO::gzip -ne 'print if /error/' access.log.gz
For production work, use the module. For quick hacks on random servers where you cannot install anything, magic @ARGV still earns its keep.
Part 10: OTHER @ARGV TRICKS
Set default files:That expands to all message logs when you pass no arguments.@ARGV = glob('/var/log/messages*') unless @ARGV; print while <>;
Filter @ARGV:
That only processes readable regular files.@ARGV = grep { -f $_ && -r $_ } @ARGV;
Add STDIN explicitly:
The dash means STDIN. Now your script works with pipes.push @ARGV, '-' unless @ARGV;
Part 11: THE FULL MONTY
Putting it all together:#!/usr/bin/env perl use strict; use warnings; # Default to common log locations if no args @ARGV = glob('/var/log/syslog*') unless @ARGV; # Handle compressed files for (@ARGV) { next unless -f; # Skip non-files s/^([^']+\.gz)\z/gzcat '$1' |/; s/^([^']+\.bz2)\z/bzcat '$1' |/; s/^([^']+\.xz)\z/xzcat '$1' |/; } # Add STDIN if nothing left @ARGV = ('-') unless @ARGV; # Process while (<>) { print "$ARGV:$.: $_" if /error/i; }
perl.gg@ARGV | +-------+-------+ | | | file file.gz - | | | open gzcat STDIN | | | +-------+-------+ | <> Transparent decompression