A useful perl command line skeleton

Dan Coutu coutu at snowy-owl.com
Tue Jun 10 15:15:40 EDT 2003


As a follow-on to my talk about Perl last month I thought I'd put 
together a handy code framework that is very useful for creating command 
line or cron based utilities.

The skeleton doesn't do much by itself but I have found myself making 
command line utilities over and over based on this same structure. It 
provides easy command line parsing as well as built-in online help and 
self-documentation by using pod. Oh, and it also provides a convenient
version variable that is CVS friendly so that you don't have to update
versions by hand each time you check your code into CVS.

You can get details for each of the packages used by using the perldoc
command like this:

perldoc Getopt::Long
perldoc Pod::Usage

and so forth.

I hope this helps those of you who are trying to make sense of perl.
-- 

Dan Coutu
Managing Director
Snowy Owl Internet Consulting, LLC
http://www.snowy-owl.com/

-------------- next part --------------
#!/usr/bin/perl -w
#
# Author:
# Created:
#
# Describe the purpose of the code here.
#

use strict;
use Getopt::Long;
use Pod::Usage;

# Provide a version variable that works nicely with CVS
our $VERSION = (qw( $Revision: 1.0 $ ))[1];

# Command variables
my $Help;
my $Verbose = 0;

# Parse any command line switches.
GetOptions("help|h|?" => \$Help,      # A flag (boolean) value
	   "verbose|v=i" => \$Verbose # An integer value
	   ) || pod2usage(2);

pod2usage(1) if $Help;

# Basic verbosity shows at least the program version to the user.
# By comparing for verbosity > some value each bit of output
# can be fine tuned into different verbosity levels.
print "myprogram version $VERSION\n" if $Verbose > 0;

__END__
=head1 myprogram

Describe your program briefly.

=head1 SYNOPSIS

myprogram [options]

=head1 OPTIONS

=over 8

=item B<--help>

Show this description of the program's use.

=item B<--verbose>

Specifies a positive integer value of zero or greater that controls 
the level of verbose detail to be provided by the program. Higher numbers
provide more detail.

The default is zero if the switch is omitted.


More information about the gnhlug-discuss mailing list