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

6. The @ARGV Array

One can use the @ARGVvariable 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>);
close(
$in);
close(
$out);

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 shiftto draw arguments out of @ARGVone at a time. When used without parameters outside of functions, shift extract arguments out of @ARGV.


Written by Shlomi Fish