what does a computer programmer doWhen using Perl, it is in a special array called argv where command line arguments are stored. To access the command line arguments of your script, you just need to read from argv.

When writing a script such as programming.pl in Perl, your users can use perl programming.pl to run the scripts on the command line.

Users can also use Perl programming.pl-a—machine remote/etc to pass any command line argument as well as other strings mentioned in this article about Perl functions. These values will automatically be disregarded by Perl. How will you know which of the script values were passed and which were not? This is where argv comes in.

An array called argv is automatically provided by Perl, holding all command line values. You won’t have to declare variables when using Perl argv. This is one variable that is always existent and the command line values are placed automatically in this variable each and every time, as you can see in this Perl course.

The array will be empty when there are no parameters. If there is one command line parameter, this value becomes the only argv element. The following elements will be contained in argv in the above example: /etc, remote, machine, -a. Here it is in action:


Here is the output when you run the perl programing.pl-a-machine remote/etc:


To point out the argv content, we used the Data::Dumper function called ‘Dumper.’ If you are using another language for programming, you might wonder where the Perl program name is.

Extracting Argv Command Line Arguments

Argv happens to be a regular Perl array. When creating other arrays, the difference is that it is populated by Perl when starting your script and that you won’t need to declare it. You can handle argv as a regular array. You can access this using the argv[0] index. You can also gain access using the foreach elements. On argv, you can also use push, pop, unshift or shift.

You can even change the argv content as well as fetch it. If you expect single values to appear on the command line you can check if it was provided by looking at $argv[0]. When you are looking for more than one variable, then check $argv[1].

Perl Argv Example

Let’s say you are creating a phone book. If you provide just 1 name, the corresponding phone number will be printed by the application. If you give a number plus a name, the program saves this pair. You can then expect parameters arriving in $argv [1] and $argv [0] but other than being your array elements, these have no other meaning. For this reason, in your code you might want to use your own variable names rather than $argv [0] and the like. Here is an example of copying each value to a corresponding variable with names represented:


You can also do it this way:


Here you can see the complete array:


Check to see if names were provided after you copy argv values. If not, exit the script. If there were names, find out if there were corresponding numbers and if so, save these to the database and exit. On the other hand, should there be no numbers, try fetching this from the database. Here is how it works—note that you don’t really type the $ sign, as this is just a prompt marker:


You can see here that all the calls look okay except for the last one. The last call saved the John Doe phone number to be Doe rather than 789. This is usually the case no matter what language you use, as you shall soon see in this Linux course as well. The command line or shell where you are running the script takes the lines apart, passing each value to Perl that arranges them in argv.

Both the Windows Command Line and the Linux/Unix Shell also split values each time a space is detected, as you can see in this Linux course. When you type John Doe 789 in perl programming.pl, what actually happened was that the shell passed three values to the script. To make this work the right way, you will need to arrange each value with spaces inside quotation marks:

$0 Variable in Perl

In the above example, the name of the executed program is always in Perl’s $0 variable.

Those familiar with Linux/Unix shell programming will immediately know that the name of the script will be in $0, the way it is with Linux/Unix. However, the rest of the command parameters are n $1 and $2. Regular Perl expressions use these variables. It is in argv where you will find command line arguments, just like the Linux/Unix shell’s $*.

If you happen to be familiar with the programming language in C, this is not unlike the language used in argv except that Perl argv does not contain the program name. Rather, it is in the $0 variable that you will find it. Since you can easily get a argv array’s number of elements by putting the array in scalar content or by using the scalar function, variables like argc are not necessary, just like it shows you in this Perl course for beginners.

Command Line students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo