The @ARGV Array

Contents | Up | Prev | Next

# 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 I, "<", $filename;
    open O, ">", $filename.".bak";
    print O join("",<I>);
    close(I);
    close(O);

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`.

Contents | Up | Prev | Next