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


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

3

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