Scriptvorlage für Perl-Scripts

Die Begründung, warum ich eine Vorlage mache, findet sich an einer anderen Stelle. Hier stelle ich meine Vorlage für Perl vor.

Beschreibung

Dieses einfache Testscript hat nur wenige Parameter. Es unterstützt sowohl Kurz- als auch Langform von Parametern. Die Dokumentation kann mit --help ausgegeben werden. Das Ergebnis ist etwas wie das hier:

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.

--param und --switch sind nur einfache Beispiele für Parameter. Alle Parameter, die nicht von Getopt::Long verarbeitet werden, werden einfach nur ausgegeben. Dabei können Optionen und weitere Parameter bunt gemischt werden. Ein Beispielaufruf wäre wie folgt:

sh> ./template.pl -y some -vv text --verbose between -x abc other parameters
Verboselevel: 3
Param: "abc"
Switch: 1

some
text
between
other
parameters

Vorlage

Aber genug Beschreibung: Die Vorlage kann hier heruntergeladen werden. Das Script sollte für das Verständnis ausreichend kommentiert sein. Ansonsten bitte einfach nachfragen.

#!/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:

Gute Perl-Module

Auf dem CPAN sind extrem viele Perl-Module zu finden. Glücklicherweise gibt es aber auch Listen, die gute Module vorschlagen. Eine solche Liste ist im CPAN unter Task::Kensho zu finden.

Für einfache Konsolen eignet sich sehr gut Term::ShellUI.