◈ Netsurfer / perl.gg / Magic Argv

Magic Argv

2025-12-20

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:

access.log access.log.1 access.log.2.gz access.log.3.gz access.log.4.gz
Some files are plain text and some are compressed. You need to grep through all of them.

The ugly way looks like this:

cat access.log access.log.1 zcat access.log.*.gz
That requires two commands, a wrapper script, or a shell loop. The one-liner below handles both in a single pass:
perl -e ' s/^([^'"'"']+\.gz)\z/gzcat '"'"'$1'"'"' |/ for @ARGV; print while <>; ' access.log*
One command handles both formats. The diamond operator sees pipes and plain files and treats them the same way.

Part 1: THE TRICK

Before the diamond operator runs, we rewrite @ARGV:
s/^([^']+\.gz)\z/gzcat '$1' |/ for @ARGV;
Files ending in .gz become:
access.log.2.gz -> gzcat 'access.log.2.gz' |
That trailing pipe tells Perl to run the command and read its output. The diamond operator sees the pipe, executes 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:
ARGV ENTRY PERL DOES ---------------- ---------------------------------- filename open(ARGV, '<', 'filename') command | open(ARGV, '-|', 'command') | command open(ARGV, '|-', 'command')
A trailing pipe means read from that command. A leading pipe means write to that 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

s/^([^']+\.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
Why use [^']+ 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:
s/^([^']+\.(?:gz|Z))\z/gzcat '$1' |/ for @ARGV;
Handle .bz2 too:
s/^([^']+\.bz2)\z/bzcat '$1' |/ for @ARGV; s/^([^']+\.(?:gz|Z))\z/gzcat '$1' |/ for @ARGV;
Two substitutions handle three formats.

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:
#!/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;
Now you can run:
zperl -ne 'print if /error/' access.log*
That handles all compression formats transparently.

Part 6: GREP ALL LOGS

Search through rotated logs:
perl -e ' s/([^'"'"']+\.gz)$/gzcat '"'"'$1'"'"' |/ for @ARGV; print "$ARGV: $_" while <>; ' /var/log/syslog* | grep -i error
The $ARGV variable tells you which file or pipe you are reading from.

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:
1. End single quote: ' 2. Add escaped single quote: "'" 3. Resume single quote: '
The result '"'"' produces a literal single quote in the final string.

Inside a script file, the escaping disappears:

s/^([^']+\.gz)\z/gzcat '$1' |/ for @ARGV;
No shell escaping is needed.

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:

>file # Opens for writing |command # Pipe to command command| # Pipe from command
If someone controls your filenames, they control your system.

A safer version looks like this:

for (@ARGV) { if (/\.gz$/ && -f $_) { # Must be a real file $_ = "gzcat \Q$_\E |"; # \Q quotes metacharacters } }
The -f check confirms it is a regular file. The \Q...\E escapes any shell metacharacters.

Part 9: MODERN ALTERNative

PerlIO::gzip does this cleanly:
perl -MPerlIO::gzip -ne 'print if /error/' access.log.gz
That requires installing a module. The @ARGV trick works with stock Perl.

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:
@ARGV = glob('/var/log/messages*') unless @ARGV; print while <>;
That expands to all message logs when you pass no arguments.

Filter @ARGV:

@ARGV = grep { -f $_ && -r $_ } @ARGV;
That only processes readable regular files.

Add STDIN explicitly:

push @ARGV, '-' unless @ARGV;
The dash means STDIN. Now your script works with pipes.

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; }
@ARGV | +-------+-------+ | | | file file.gz - | | | open gzcat STDIN | | | +-------+-------+ | <> Transparent decompression
perl.gg
✓ Document: Doneone-liners / 12.3 KiB▧