in reply to option on command line
Now your script will accept options from the command line, storing them in globals and complaining if you use options without their mandatory argument (i.e., you can't use a bare -u on the command line).#!/usr/bin/perl use strict; use Getopt::Mixed; use vars qw|$opt_u $opt_x $opt_m|; # Note that you have to use globals... Getopt::Mixed::getOptions( qq|u=s x:i m:i| ); # Option u wants (mandatory) a string # Option x accepts (not mandatory) an integer # Option m too... die "Hey man, you have to provide an username!\n" unless $opt_u ; print "Option 'u' is $opt_u\n";
What makes Getopt::Mixed interesting is the possibility to specify longer synonyms for options. So, you can change a line of the script in this way:
username is a synonym for u, so that you can call your script from commandline is these two ways with the same results:Getopt::Mixed::getOptions( qq|u=s username>u x:i m:i| );
# getopt.pl -u larsen Option 'u' is larsen # getopt.pl --username larsen Option 'u' is larsen
Update: Added an error message in case the user does not provide -u option
|
|---|