How can I read in a variable/value which is a runtime parameter that is inside a constant list of hashes?

我与影子孤独终老i 提交于 2019-12-14 03:27:09

问题


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.


回答1:


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

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);



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



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

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


来源:https://stackoverflow.com/questions/35918285/how-can-i-read-in-a-variable-value-which-is-a-runtime-parameter-that-is-inside-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!