Getopt::Longをつかったperlスクリプトのサンプル

#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;
use FindBin;
my $mypath = $FindBin::RealBin;
#die "$mypath\n";
use File::Basename;
my ($myname) = fileparse($0);
use Threads;

#
# Option handling
#
my $help = '';          #Default : false
my $verbose = '';       #Default : false
my $debug = '';         #Default : false
my $thread;                     # --thread 10

GetOptions (
        'help' => \$help
        , 'verbose' => \$verbose
        , 'debug' => \$debug
        , 'thread=i' => \$thread
                ) or die $!;
if ($help) {
        &showhelp();
        exit;
}

#
# Main
#
&verbose_msg if $verbose;







#
# Sub routines
#
sub verbose_msg(){
        print "\$help = 1\n" if $help;
        print "\$verbose = 1\n" if $verbose;
        print "\$mypath = $mypath\n";
        print "\$myname = $myname\n";
}
sub showhelp() {
        print <<"ENDOFHELP";
 ************************* How to use *************************
$mypath/$myname [options] [-- other_arguments_for_script]
options:                Processed by Getopt::Long
        -help           Displays this help message.
        -verbose        Displays more information.
        -debug          Debug mode. More and more information.
        -thread n       Number of thread concurrently run.

 **************************************************************
ENDOFHELP
}