Ninthwave has asked for the wisdom of the Perl Monks concerning the following question:
I have discussed command line argument processing before. And was given some great code. Now at this point I want to have a script that can be called with options to either add a record, edit a record, list all records or view a record. But you should only enter one choice. This seems to be working for me. For people with more experience with GetOpt::Long is there a better way to do this. I know better is subjective but I like comparing other ways to my ways so I can break any bad habits I may be forming. My main query is should I do the long xor statement check, is that check adequate and am I missing something in Getop::Long that marks entries as being required or selective.
Update:Moved intialise variables to the proper place in the code, bad editting by me. This is how the code looks that I am testing. Noting that if three options are choosen the if statement returns true.use strict; use Getopt::Long; use Pod::Usage; #******************************************************** #** Intialise variables #******************************************************** my $Add_Record= 0; my $Edit_Record= 0; my $List_Records = 0; my $View_Record = 0; my $Manual = 0; my $Help = 0; #******************************************************** #** Process command line arguments. #******************************************************** GetOptions( 'add|a=s' =>\$Add_Record, 'edit|e=s' =>\$Edit_Record, 'list|l=s' =>\$List_Records, 'view|v=s' =>\$View_Record, 'man|m' =>\$Manual, 'help|h|?' =>\$Help ); #******************************************************** #** Print Usage files based on the status of the command #** line arguments. #******************************************************** pod2usage(1) if $Help; pod2usage(-exitstatus => 0, -verbose => 2) if $Manual; if (($Add_Record xor $Edit_Record) xor ($List_Records xor $View_Record +)) { # Run Program } else { # Print manual }
|
|---|