For program (not library a.k.a. module) code. Provides either succinct or verbose "help" (usage) messages when program code includes self-documenting POD.
use Pod::Usage 'pod2usage'; BEGIN { my $max_valid_args = <FILL IN FOR YOUR PROGRAM> ; my $min_valid_args = <FILL IN FOR YOUR PROGRAM> ; my( $argC,$argN ) = (0 + @ARGV , $ARGV[$#ARGV] ); if( grep { $_ eq lc($argN) or $_ eq substr lc($argN)=>1 } qw/ -h -u -man -help -usage / ) { pod2usage(-verbose => 2) } elsif ($argC < $min_valid_args or $argC > $max_valid_args ) { pod2usage(1) } }

This snippet is a kwik-N-eezy way to provide rudimentary yet flexible user help messages for your Perl program. It is not strictly necessary to wrap it in a BEGIN sub block, so if you don't like that, use it bare. It incurs some small additional overhead to do like shown here. I don't care about that and like the scoping confinement provided for the logic, as well as the chance to prevent loading some large module I'm use'-ing if all the user wanted was a usage hint.

The excellent Getopt::* module facilities are not employed here to do the argument parsing. That keeps this snippet a lightweight and brief way of providing help messages. I am in no way discouraging people from using Getopt::*, quite the contrary. I urge you to use that reusable and well-engineered solution. However, if, like me, you prefer to keep the source for simple Perl programs looking very, er, simple, then try this snippet out.

The flexibility mentioned comes in because the user can type either one-hyphen - or two-hyphen -- forms of multi-letter switches and get the response they expect.

Soren A / somian / perlspinr / Intrepid

-- 
Try my n.y.p.m.blue Perl Monks CSS Theme
(edit "On-Site CSS Markup" in your User Settings)

In reply to Usage via Pod::Usage accounting for user variation by Intrepid

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.