Getopt::Long

Getopt::Long を使ってコマンドラインパラメータを取得するメモ。

=head1 SYNOPSIS

    test.pl [options]

    Options:
        [--help|-?] put help message
        [--host|-h] host name
        [--port|-p] port number

=cut

use strict;
use warnings;

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

use Data::Dumper;

Readonly my @OPTIONS => (
    q{help|?},
    q{host|h=s},
    q{port=i},
);

Readonly my @DEFAULT_OPTIONS => (
    help => 0,
    host => q{XXX.XXX.XXX.XXX},
    port => 80,
);

my %option_for = ( @DEFAULT_OPTIONS );
GetOptions( \%option_for, @OPTIONS ) or pod2usage(2);
pod2usage(1) if $option_for{help};

print Dumper( \%option_for ), "?n";

上記の例だと・・・ 下記のような呼び出しが可能。

test.pl -h xxx.xxx.xxx -p 80
test.pl -host xxx.xxx.xxx -port 80
test.pl --host xxx.xxx.xxx --port 80
test.pl --host=xxx.xxx.xxx --port=80
test.pl -?
test.pl --help

Getopt::Long も Pod::Usage も多機能なので、詳しくは CPAN 参照の事。