Perl Command Line Arguments

Command line arguments are sent to a Perl program in the same way as in any other language. The @ARGV array holds the command line argument. There is no need to use variables even if you use "use strict". By default, this variable always exists and values from the command line are automatically placed inside this variable. To access your script's command-line arguments, you just need to read from @ARGV array.

Perl allows using @ARGV array as filenames by using <>. The $ARGV contains the name of the current file when reading from <>.

@ARGV array elements

The @ARGV array works same as a normal array. Its first argument will be $ARGV[0], second $ARGV[1], and so on.


Perl Command Line Argument Example

Let's see a simple example to print command line arguments. In this example, we will print a welcome message with the users name as the argument from the command line. We need two command line arguments as user's first and last name.

Step1 Create a script welcome.pl in your system.

In the above script, first it will check whether two command line arguments are provided or not. If not, it will give the wrong entry error. And if yes, it will process those arguments as $ARGV[0] and $ARGV[1] respectively.

Step2 Run the script in your console as perl welcome.pl Christian Grey

Note: Before giving this command, make sure you are on the directory containing your script welcome.pl

Perl Command line arguments 1

This is the output you will get in your console.

Step2 Run the script in your console as perl welcome.pl Christian

Here, we are passing only one argument from the command line.

Perl Command line arguments 2

In the output, you can see the wrong entry message for passing one argument.


Perl Command Line Arguments Example using Loop

Now we will run a loop to print the command line arguments. In this example, you can enter as much argument as you wish.

Step1 Create a script loop.pl in your system.

Step2 Run the script in your console as perl loop.pl a b c d e f g h

Perl Command line arguments 3

Look at the output above, it also displays total arguments passed on command line. Here we have passed 8 arguments.


Perl Getopt::Long

The simple command line options are done using ?s option. Complex command line options are done using Getopt::Std and Getopt::Long.

Getopt stands for GetOptions. It processes the content of @ARGV based on the configuration we give to it. It returns true or false value based on the processing.

Example:

In this example, we well get the age of the user from the command line.

We have declared a variable $x which will store value inserted into --from from the standard console. The from=s declares command line parameter called --from with a string after it. It is mapped further to the variable $x. The backslash (\) means we are passing a reference to the variable.

This script will switch to die part only when we will run this script by passing something that looks like a parameter name and starts with a (-) but is not declared in this script.

Now, we will run this program.

Output:

Perl Command line arguments 4

Look at the output, after entering age on the console, we got the above output.





Latest Courses