"Perl for Perl Newbies" - Part 2The @ARGV Array

6. The @ARGV Array

One can use the @ARGV variable to access the command line arguments passed to the perl script at the command line. Here's an example, that makes a backup of the file specified as its argument:

use strict;
use warnings;

my $filename = $ARGV[0];

open my $in, "<", $filename;
open my $out, ">", $filename.".bak";
print {$out} join("",<$in>);

Using the command-line for specifying parameters to the program is usually more handy than using files, or modifying the script each time.

Note that it is often convenient to use shift to draw arguments out of @ARGV one at a time. When used without parameters outside of functions, shift extract arguments out of @ARGV.

Written by Shlomi Fish