首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Perl::Declare参数操作未调用

Perl::Declare参数操作未调用
EN

Stack Overflow用户
提问于 2014-09-29 13:59:43
回答 1查看 75关注 0票数 1

我在脚本中使用Getopt::Declare,但是调用脚本和传递-get_ip "test"不会做任何事情,即脚本执行"my“语句,而getFirstAvailableIP没有被调用。

代码语言:javascript
复制
use Getopt::Declare;
use lib "/home/vtsingaras/NicTool/client/lib/";
use NicToolServerAPI;
use strict;
use warnings;
#debug remove
use Data::Dumper;

#NicToolServer settings, edit
my $ntconf = {
    ntuser => 'censored',
    ntpass => 'censored',
    nthost => 'censored',
    ntport => 8082,
};

my ( $zone, $fqdn, $ip, $comment );

my $options_spec = q(+g[et_ip] <zone>   Get the first available IP from the provided reverse <zone>.
                        {getFirstAvailableIP($::zone);} 
+s[et_dns] <fqdn> <ip> <comment>    Create an A record for <fqdn> that points to <ip> and the associated PTR record.
{createFwdAndPtr($::fqdn, $::ip, $::comment);}  
    );
my $args = Getopt::Declare->new($options_spec);
#Setup NicTool
my $nt = new NicToolServerAPI;
$NicToolServerAPI::server_host   = $ntconf->{nthost};
$NicToolServerAPI::server_port   = $ntconf->{ntport};
$NicToolServerAPI::data_protocol = "soap";
#$NicToolServerAPI::use_https_authentication = 0;

sub nt_login {
    #Login to NicTool Server
    my $ntuser = $nt->send_request(
        action   => "login",
        username => $ntconf->{ntuser},
        password => $ntconf->{ntpass},
    );
    if ( $ntuser->{error_code} ) {
        print( "Unable to log in: " . $ntuser->{error_code} . " " . $ntuser->{error_msg} . "\n" );
        exit 1;
    } else {
        print( "Logged in as " . $ntuser->{first_name} . " " . $ntuser->{last_name} . "\n" );
    }
}

sub getFirstAvailableIP {
    my $fqdn = $_[0];
    print $fqdn;
    die "blah";
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-29 19:01:09

问题是您指定了+,而不是在$options_spec中为get_ip指定了-

下面是一个自包含的可运行示例,它调用getFirstAvailableIP

代码语言:javascript
复制
use strict;
use warnings;
use Getopt::Declare;

my $zone;

my $args = Getopt::Declare->new(<<'END_OPTS');
    #               tab
    #               ||||
    #               vvvv
    -g[et_ip] <zone>    Get the first available IP from the provided reverse <zone>.
        { getFirstAvailableIP($zone); }
END_OPTS

print "hello world\n";

exit;

sub getFirstAvailableIP {
    print "blah - @_\n";
}

__END__

并执行:

代码语言:javascript
复制
$ perl declare_test.pl -get_ip test
blah - test
hello world

请注意,该模块在其规范中需要一个tab字符;这使得正确复制“n”粘贴变得困难。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26101700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档