Hello esteemed monks. :)

I have a script that I use Getopt with to call certain loops. It looks like so:

use Getopt::Long; use strict; use Data::Dumper; use IO::File; my ($read,$generate,$help); # option switches GetOptions ( "help" => \$help, "read" => \$read, "generate" => \$generate, );


Each one just points to a script that I put in an if loop, like so:
if ($read) { - code snippets - }

I added a few extra switches for single characters, like so:
my ($read,$generate,$help); # option switches GetOptions ( "h" => \$help, "help" => \$help, "r" => \$read, "read" => \$read, "g" => \$generate, "generate" => \$generate, );

What I'm curious to know is if there's a better way to do this. Right now running 'scriptname -read' or 'scriptname -r' do what I'd like them to do, but, for example, while 'scriptname -generate' does the same as 'scriptname -g', 'scriptname -gen' fires me back an unknown option error.

I'm assuming that I need to somehow use (chomp, perhaps?) on the input to just snip whatever the -option is down to the first letter, but I'm not familiar enough with Getopt to know how to accomplish this.

Any advice is as always really appreciated, and thanks again to everyone on here for all the help you've given me so far. :)

In reply to Need help using Getopt, please. by Seventh

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.