Search code examples
perlerror-handlingconstantssubroutinehash-of-hashes

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


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.


Solution

  • 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