namishtiwari has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a my script here--
print "The Perl Script does the User health check and system health ch +eck...\n"; print "--------------------------------------------------------------- +------\n"; # use strict; my($OS); $OS = $^O; # need to test @ARGV before GetOptions shifts it if (@ARGV == 0) { print "\nNo options provided, using defaults (use -h to view option +s)\n"; } #GetOptions( 'h|help' => \$_help # ,'u|userdata' => \$_userdata # ,'s|systemdata' => \$_systemdata # ,'a|authresponsecode' => \$_authresponsecode # ,'o|outpur_dir' => \$_output_dir); #if( $_help ) { # printUsage(); # automatically exits the program #} use Getopt::Std; ###################################################################### +######## # Print the usage info and automatically exit ###################################################################### +######## sub printUsage() { print "\n\nUsage:"; print "\n perl MSLogStat.pl [options] [logfile1] [logfile2] ... [l +ogfile(n)]"; print "\n\nOptions:"; print "\n -h * display usage help"; print "\n -o * redirect the output to a directory"; print "\n -u * display user related data"; print "\n -a * display auth response codes"; print "\n -s * output only system related data"; print "\n"; exit; } ###################################################################### +######## # Print the authresponsecode ###################################################################### +######## sub authResponseCode () { print "AAFW_AUTH_SUCCESS = 0"; print "AAFW_AUTH_MORESTEPS = 1"; print "AAFW_AUTH_ID_NOT_FOUND = 2"; print "AAFW_AUTH_INVALID_CREDENTIAL = 3"; print "AAFW_AUTH_ACCOUNT_EXPIRED = 4"; print "AAFW_AUTH_ACCOUNT_INACTIVE = 5"; print "AAFW_AUTH_ATTEMPTS_EXHAUSTED = 6"; print "AAFW_AUTH_TOKEN_EXPIRED = 7"; print "AAFW_AUTH_CHALLENGE_EXPIRED = 8"; print "AAFW_AUTH_INVALID_REQUEST = 9"; print "AAFW_AUTH_CRED_REISSUED = 10"; print "AAFW_AUTH_INTERNAL_ERROR = 11"; print "AAFW_AUTH_UNSUPPORTED_MECH = 12"; print "AAFW_AUTH_LAST = 13"; } # redirect output to a directory: if( $_output_dir ) { # does this directory already exist? if (! -e $_output_dir) { if( $OS =~ "Win32" ) { `mkdir $_output_dir`; } else { `mkdir -p $_output_dir`; } } if (! -e $_output_dir) { die "Failed to create output directory $_output_dir\n"; } } if( $_authresponsecode ) { print "Printing the Auth Response Codes"; authresponsecode(); } sub userData() { #$LOG = $_logfile_name; $username=$2; open($LOG,"logfile.txt") or die "\nUnable to open log file:\n"; @stock = <$LOG>; @matches = (grep(/$username/, @stock)); print @matches; } my $opt = "hu:sao"; my %options; getopts( $opt , \%options ); printUsage() if defined $options{h} ; authResponseCode() if defined $options{a} ; userData() if defined $options{u} ;
in function userData() i want to make changes so that my script take username as command line arguement and logfile name also to be entered from command line,it can be multiple log files. Please advice me how can i do that. Please do suggest me if you find something to be changed here to work it more efficiently. Thanks NT

Replies are listed 'Best First'.
Re: passing command line arguements to perl script
by DStaal (Chaplain) on May 29, 2009 at 14:51 UTC

    General comments:

    Line 3 should be line 1. And it should be uncommented. A use warnings; as line 2 would be good as well. Together these two lines will help you find more problems in your Perl code than just about anything else ever could.

    Line 20 should be before line 11. And, given what you are asking, it should probably read Getopt::Long instead of Getopt::Std. (By the way, that's the answer to your direct question: Getopt::Long can do everything you ask.)

    Exception to the above: While you can make multiple logfile arguments, I'd rather use the <> operator, and have the script take a list of filenames on standard input. (Which also lets it read from a pipe instead. Very useful if you decide to start compressing these files, or only want to process certain lines...)

    Learn POD, and use it (and Pod::Usage) instead of trying to format your own helptext. It's much harder to do right than you'd think. POD makes it easy.

      I have to agree with Dstaal. Not only does Pod::Usage tell you how to use it, but if you'll scroll down to where the header is "Recommended Usage", the example below is almost a template on how to handle your issue.