Command Line Functionality


Just like in C++, or JAVA sometimes it is a lot easier to have a program that will accept command line arguments. Adding this feature is extremely easy in Perl, all that is needed is variable to get the vale! It is also possible to get the number of arguments being passed, although it is not as easy as it sounds. Just like for other things, you should start counting at 0, so when you pass a single argument the $#ARGV will be 0. If you didn't pass any arguments it would be -1. Here is a very brief example of how to get use a command line argument in Perl:

      #!/usr/bin/perl -w

      $number1 = $ARGV[0];

      $number2 = $ARGV[1];

      #Note that this will return 1 less than
      # the number of arguments (starts counting
      # at 0).  If you want the true number, 
      # add 1.

      $num_args = $#ARGV;

      $num_args++;

      print $number1 + $number2, "\n\n";

      print "Number of Arguments: ", $num_args, "\n";

      
Running the program looks like this:
 
      zurawskij@pegasus.montclair.edu 45% ./add.pl 5 11
      16      
      
      Number of Arguments: 2
      

Using this trick on some old examples, we can read any specified file, instead of hard coding the value in:

      #!/usr/bin/perl -w

      use Fcntl ':flock';

      $file = $ARGV[0];

      open(INFILE, $file) or die "File Not Found";

      flock(INFILE, LOCK_EX);

      while (<INFILE>) {
        print "Line: ", $_;
      }

      flock(INFILE, LOCK_UN);

      close(INFILE);
      
Running the program looks like this:
 
      zurawskij@pegasus.montclair.edu 45% ./read.pl read.pl
      Line: 	  #!/usr/bin/perl -w
      Line:
      Line:       use Fcntl ':flock';
      Line:
      Line:       $file = $ARGV[0];
      Line:
      Line:       open(INFILE, $file) or die "File Not Found";
      Line:
      Line:       flock(INFILE, LOCK_EX);
      Line:
      Line:       while () {
      Line:         print "Line: ", $_;
      Line:       }
      Line:
      Line:       flock(INFILE, LOCK_UN);
      Line:
      Line:       close(INFILE);      
      

One last example incorporates the usage of the eval() function.

      #!/usr/bin/perl -w

      $number1 = $ARGV[0];
                                                                                
      $op = $ARGV[1];
                                                                                
      $number2 = $ARGV[2];
                                                                                
      print eval($number1 . $op . $number2), "\n";
      
Running the program looks like this:
 
      zurawskij@pegasus.montclair.edu 45% ./calc.pl 5 + 5
      10
      zurawskij@pegasus.montclair.edu 45% ./calc.pl 5 - 5
      0
      zurawskij@pegasus.montclair.edu 45% ./calc.pl 5 / 5
      1        
      

We have seen many of the interesting and useful features in Perl, but now we move on to one of the unique features, regular expressions.


Subroutines    <Command Line>    Regular Expressions

Created By: Jason Zurawski
Last Modified: Feb. 29, 2004