2

我编写了以下代码:

my $version = sub {
    print "$PROGNAME $VERSION - $AUTHOR\n";
    exit 0;
};

my $usage = sub {
    print "Usage: proll <options>\n";
    print "Available options:\n";
    print " -h, --help  Print this help and exit.\n";
    print " --version   Print version.\n";
    print " XdY     Launch X dice with Y faces.\n";
    exit 0;
};

my $ret = GetOptions ( "version" => \$version,
                       "h|help" => \$usage );

但是,如果我调用脚本--version--help它不调用子例程。我哪里错了?

如果我按如下方式更改代码,它总是调用第一个子程序,也没有任何命令行参数:

my $ret = GetOptions ( "version" => &$version,
                       "h|help" => &$usage );
4

1 回答 1

7

\$version是对 的引用$version,其中$version是对匿名子例程的引用;所以,\$version是对子程序的引用的引用。这太间接了。您只需要一个单一级别的参考性:

my $ret = GetOptions ( "version" => $version,
                       "h|help" => $usage );
于 2012-10-09T21:21:18.940 回答