Most Perl books start with a “Hello, World” tutorial. That’s probably the simplest example to start with for most programming languages. But finding out how many parameters are in the commandline to a Perl script is also simple, and very important to most of the tutorials on this site.

I should note beforehand that some Perl installations do not include the name of the Perl script as part of the parameter list, but some do. I will assume, unless otherwise noted, that it is not included.

Here is a Perl script to determine the number of commandline parameters:

#!/usr/bin/perl
	
####################
#
# FILE: params01.pl
#
####################
	
use strict; # Declare strict checking on variable names, etc.
	
my $argc;   # Declare variable $argc. This represents
            # the number of commandline parameters entered.
	
$argc = @ARGV; # Save the number of commandline parameters.
if (@ARGV==0)
{
  # The number of commandline parameters is 0,
  # so print an Usage message.
  #
  usage();  # Call subroutine usage()
  exit();   # When usage() has completed execution,
            # exit the program.
}
	
print "Hello, there!\n";
print "You have $argc parameters\n";
	
my $i = 0; # Declare and set a loop index
	
foreach my $parm (@ARGV)
{
  print "parameter $i: ",$ARGV[$i],"\n";
  $i++;
}
exit(0);
	
sub usage
{
  print "Usage: perl params01.pl param01 [param02 ... param0n]\n";
}

This is the first in a series of Perl scripts dealing with mostly with data file manipulation. Please feel free to leave comments if there is something that you would like to see. I cannot guarantee that I’ll be able to answer immediately, but I will answer as soon as I can.