2

Possible Duplicate:
How can I pass command-line arguments to a Perl program?

I have code that parses an input file and outputs a .stat file and a .csv file. What do I need to code to run my Perl Script from a command prompt like this?

perl myprogram.pl -i "C:\temp\inputfile.txt" -o "C:\temp\myoutput.csv" -s "C:\temp\myoutput.stat"

Where the -i is the input file (required) and the -o is the output file location for a CSV file (required) and the -s is the output file for a .stat file (optional). I already have the code for the program but need to implement the arguments so that they can run in command line.

1
  • muh, linked post doesn't mentioned checking for required arguments at all. How is that an exact duplicate??? Commented Jul 10, 2012 at 19:37

1 Answer 1

4
use Getopt::Long qw( GetOptions );

sub usage {
   print STDERR @_ if @_;
   print STDERR ...;
   exit(1);
}

sub help {
   print ...;
   exit(0);
}

my $input_qfn;
my $csv_qfn;
my $output_qfn;

GetOptions(
   "help|h|?" => \&help,
   "i=s"      => \$input_qfn,
   "o=s"      => \$csv_qfn,
   "s=s"      => \$stat_qfn,
) or usage();

defined( $input_qfn )
   or usage("-i option is required\n");
defined( $csv_qfn )
   or usage("-o option is required\n");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.