如何读取变量/值,该变量/值是在常量列表中的运行时参数? [英] How can I read in a variable/value which is a runtime parameter that is inside a constant list of hashes?

查看:156
本文介绍了如何读取变量/值,该变量/值是在常量列表中的运行时参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将变量放入字符串中,但是在运行程序时它显示为空白。这是我正在使用的示例:

I have tried putting the variables in the string but it reads as blank when I run the program. Here is an example of what I'm working with:

        use constant {
    #list will contain more errors

    ERROR_SW => {
    errorCode => 727,
    message => "Not able to ping switch $switch_ip in $timeout seconds",
    fatal => 1,
    web_page => 'http://www.errorsolution.com/727',
    }
};

sub error_post {
    my ($error) = @_;
    print($error->{message});   
}
    error_post(ERROR_SW);

我只想发布包含字符串中变量值的错误。

I simply want to post the error with the variable values included in the string.

推荐答案

如前所述,您的 ERROR_SW 常数 ,并且可能不包含运行时变量

As has been explained, your ERROR_SW is a constant, and may not contain run-time variables

如果您打算 $ switch_ip $超时也是常量值,因为 use constant 是在编译时求值的,所以您还必须事先声明和定义这两个变量。像这样

If you intended $switch_ip and $timeout to also be constant values then, because use constant is evaluated at compile time, you would also have to declare and define these two variables beforehand. Like this

use strict;
use warnings 'all';

my ($switch_ip, $timeout);

BEGIN {
    ($switch_ip, $timeout) = qw/ 127.0.0.1 30 /;
}

use constant {
    ERROR_SW => {
        errorCode => 727,
        message   => "Not able to ping switch $switch_ip in $timeout seconds",
        fatal     => 1,
        web_page  => 'http://www.errorsolution.com/727',
    }
};

sub error_post {
    my ($error) = @_;
    print( $error->{message} );
}

error_post(ERROR_SW);


但是,我认为您的意思是消息会随这些变量的值而变化,而对于常量来说这是不可能的。通常的方法是定义一条错误消息,使其具有包含 printf 字段说明符的恒定错误消息字符串。像这样

However I think you meant the message to vary with the values of these variables, which is impossible with a constant. The usual way is to define an error message to have constant error message string that contain printf field specifiers. Like this, for instance

use strict;
use warnings 'all';

use constant {
    ERROR_SW => {
        errorCode => 727,
        message   => "Not able to ping switch %s in %s seconds",
        fatal     => 1,
        web_page  => 'http://www.errorsolution.com/727',
    }
};

my ( $switch_ip, $timeout ) = qw/ 127.0.0.1 30 /;

sub error_post {
    my ($error) = @_;
    printf $error->{message}, $switch_ip, $timeout;
}

error_post(ERROR_SW);



输出



output

Not able to ping switch 127.0.0.1 in 30 seconds


choroba 提示在他的评论中是将 message 字段的值作为子例程引用。可以在运行时执行以合并参数的当前值。该解决方案看起来像这样

An alternative way that choroba hinted at in his comment is to make the value of the message field a subroutine reference. That can be executed at run time to incorporate the current values of the parameters. That solution looks like this

请注意 $ error-> {message}()将引用称为调用而不是 evaluated

Note the additional parentheses at the end of $error->{message}() to call the reference to be called instead of evaluated

use strict;
use warnings 'all';

my ($switch_ip, $timeout);

use constant {
    ERROR_SW => {
        errorCode => 727,
        message   => message   => sub { "Not able to ping switch $switch_ip in $timeout seconds"},
        fatal     => 1,
        web_page  => 'http://www.errorsolution.com/727',
    }
};

($switch_ip, $timeout) = qw/ 192.168.0.1 99 /;

sub error_post {
    my ($error) = @_;
    print( $error->{message}() );
}

error_post(ERROR_SW);



输出



output

Not able to ping switch 192.168.0.1 in 99 seconds

这篇关于如何读取变量/值,该变量/值是在常量列表中的运行时参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆