Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

How can I pass command-line arguments to a Perl program

By: a guest on Feb 27th, 2012  |  syntax: None  |  size: 0.59 KB  |  views: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. script.pl "string1" "string2"
  2.        
  3. use Getopt::Long;
  4. my $data   = "file.dat";
  5. my $length = 24;
  6. my $verbose;
  7. $result = GetOptions ("length=i" => $length,    # numeric
  8.                     "file=s"   => $data,      # string
  9.                     "verbose"  => $verbose);  # flag
  10.        
  11. $numArgs = $#ARGV + 1;
  12. print "thanks, you gave me $numArgs command-line arguments.n";
  13.  
  14. foreach $argnum (0 .. $#ARGV) {
  15.  
  16.    print "$ARGV[$argnum]n";
  17.  
  18. }
  19.        
  20. foreach my $arg (@ARGV) {
  21.     print $arg, "n";
  22. }
  23.        
  24. while (my $line = <>) {
  25.   process_line($line);
  26. }
  27.        
  28. while (my $arg = shift) {
  29.   print "Found argument $argn";
  30. }