Getopt::Long

发布时间 2023-10-26 14:14:52作者: 天使不设防

perl 模块Getopt::Long使用示例

use strict;
use warnings;
use Getopt::Long;

my @ARGV2 = @ARGV;
my $leng =10;
my $data ="x.txt";
my $verb;
my $help;

die("error in options") unless GetOptions(
    "l|length=i" => \$leng,
    "f|file=s"   => \$data,
    "v|verbose"  => \$verb,
    "h|help"     => \$help,
);


sub usage {
    print     
<<USAGE
    -l|--length <number>    set number of length
    -f|--file <str>            set name of files
    -v|--verbose            set verbose
    -h|--help                 show this help information
USAGE
}

if($help || @ARGV2 == 0){
    exit 0 if &usage();
}

print "$leng\n$data\n$verb";