Skip to content

Perl: Simple way to read Command line arguments

Parsing command-line arguments in Perl is very easy.

All the command-line arguments are present in the array $ARGV. You can get the number of arguments passed as ( $#ARGV + 1 ).

Below is a sample Perl script to read the command-line parameters.

#!/usr/bin/perl
#-------------------------------------------------#
#  File:    argv.pl                               #
#  Author:  Dipin Krishna [email protected]   #
#-------------------------------------------------#
# The commandline arguements are present in the variable $ARGV.
 
$numberofArgs = $#ARGV + 1;
print "The $numberofArgs command-line arguments are:\n";
 
foreach $argnum (0 .. $#ARGV) {
    print " $argnum.\t$ARGV[$argnum]\n";
}
Dipin Krishna

Written by Dipin Krishna

Senior full-stack engineer with 15 years across Django, Laravel, SwiftUI and the infrastructure underneath. Available for contract work.

Work with me →

Leave a note

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.