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.

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 }
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.

In reply to More on Processing Command Line Arguments by Ninthwave

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.