7.8. Command line parameters

  • @ARGV - all the arguments on the command line

  • $ARGV[0] - the first argument

  • $0 - name of the program

  • perl read_argv.pl blue

Example 7-4. examples/arrays/read_argv.pl

#!/usr/bin/perl
use strict;
use warnings;

my $color;
if (defined $ARGV[0]) {
    $color = $ARGV[0];
}

my @colors = ("Blue", "Yellow", "Brown", "White");
if (not defined $color) {
    print "Please select a number:\n";
    foreach my $i (0..$#colors) {
        print "$i) $colors[$i]\n";
    }
    my $num = <STDIN>;
    chomp($num);
    if (defined $colors[$num]) {
        $color = $colors[$num];
    } else {
        print "Bad selection\n";
        exit;
    }
}

print "The selected color is $color\n";

If you are interested in on-site trainings by the author, please contact me directly.

Online courses: