Se vogliamo creare realmente script che si avvicinino il più possibile agli standard POSIX, per il perl ci viene in aiuto per quanto riguarda gli argomenti da passare ad uno script, il package Getopt::Std
Vediamo quindi uno script (ben commentato spero) che può essere usato come template per creare i propri (piccoli) programmi. L’uso è il classico:
perl esempio.pl -h -v
oppure
perl esempio.pl -hv
#!/usr/bin/perl -w
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
#
#
# Global variables
#
# Enable the options
use Getopt::Std;
$verbose=0;
use vars qw/ %opt /;
#
# Main subroutine
sub init {
# Set the options range
my $opt_string = 'hv:'; # CHANGE HERE WITH YOUR OPTIONS
getopts( "$opt_string", %opt ) or usage();
# Print the usage if the -h options is passed
usage() if $opt{h};
#
# Check and enable verbose mode
# eg. $verbose && print "additional informations or variablesn";
#
if ($opt{v}) {
print STDERR "Verbose mode ON.n";
$verbose=1;
}
}
#
# User help subroutine
#
sub usage {
####################################
print STDERR << "EOF";
This program does... # CHANGE
usage: $0 [-hv] # CHANGE
-h : this (help) message
-v : verbose output
example: $0 -v # CHANGE
EOF
####################################
exit;
}
#
# HERE STARTS THE SCRIPT
#
&init();
