Script Template for Perl

I did switch from Perl to Python when Perl 6 (now Raku) came out. So the script on this page is still for Perl 5.

The script supports short as well as long parameters. For example a --help produces the following output:

Usage:
    perl template.pl [options] [file ...]

Options:
    -h, --help              Print a brief help message and exits.

    -m, --man               Prints the manual page and exits.

    -x, --param=VALUE       A parameter.

    -y, --switch            A simple switch.

    -v, --verbose           Be more verbose. Can be used multiple times to
                            print more information.

Template

Download Script

#!/usr/bin/perl -w

# Copyright (c) 2014 Christian Mauderer <oss@c-mauderer.de>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

=head1 NAME

template.pl - template script

=head1 SYNOPSIS

perl template.pl [options] [file ...]

=head1 DESCRIPTION

B<template.pl> is a template for my scripts.

=head1 AUTHOR

Christian Mauderer <oss@c-mauderer.de>

=head1 OPTIONS

=over 24

=item B<-h, --help>

Print a brief help message and exits.

=item B<-m, --man>

Prints the manual page and exits.

=item B<-x, --param=VALUE>

A parameter.

=item B<-y, --switch>

A simple switch.

=item B<-v, --verbose>

Be more verbose. Can be used multiple times to print more information.

=back

=cut

use strict;
use warnings;

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

# ---------- Read Options
Getopt::Long::Configure ("bundling", "no_ignore_case");

my $man = 0;
my $help = 0;
my $param = '';
my $switch = 0;
my $verbose = 0;

GetOptions(
	"help|h" => \$help,
	"man|m" => \$man,
	"param|x=s" => \$param,
	"switch|y" => \$switch,
	"verbose|v+" => \$verbose,
) or pod2usage 1;

pod2usage (-exitstatus => 0, -verbose => 1) if $help;
pod2usage (-exitstatus => 0, -verbose => 2) if $man;

# ---------- Print info
print "Verboselevel: $verbose\n" if $verbose >= 1;
print "Param: \"$param\"\n" if $verbose >= 1;
print "Switch: $switch\n" if $verbose >= 1;
print "\n" if $verbose;

# ---------- Print all other arguments
print "$_\n" foreach (@ARGV);

# vim: set ts=4 sw=4:

Suggestions for Perl Modules

CPAN knows a lot of Perl-Modules. Fortunately, there are also lists with well tested or commonly used modules. One such list can be found in CPAN under the name Task::Kensho.

A module for simple shells is: Term::ShellUI.