[Chicago-talk] faster -h

Greg Fast gdf at speakeasy.net
Sat May 7 07:38:21 PDT 2005


On Sat, 07 May 2005 05:38:24 -0500, Whitney Jackson <whjackson at gmail.com> wrote:
> So I have this Perl program that uses some hefty modules and as a result
> it takes a second or two just to get going.  Most of the time I don't
> care because it's relatively long running and the loading time isn't
> noticeable.  However, it's annoying to have to wait when I just want
> usage information.  So I'm wondering if there's some way to do the
> GetOptions stuff up front and then avoid the "use SuchAndSuch;" when I
> find a -h or usage violation.

Put the getopt and -h check in a BEGIN block before you do any of the
other "use"s:

    #!/usr/bin/perl -w
    use strict;

    our %opts; # make %opts globally visible
    BEGIN {
      use Getopt::Std;
      getopts( 'hx:y:z', \%opts );
      if ( defined($opts{h}) ) {
        print STDERR "Usage: blah blah blah\n";
        exit(-1); # [1]
      }
    }
    use Big::Ugly::Module;
    # ...

[1] if you die() inside a BEGIN block, you'll get a "BEGIN
failed--compilation aborted" error.

--
Greg Fast
http://cken.chi.groogroo.com/~gdf/


More information about the Chicago-talk mailing list