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.
As has been explained, your
ERROR_SW
is a constant, and may not contain run-time variablesIf you intended
$switch_ip
and$timeout
to also be constant values then, becauseuse constant
is evaluated at compile time, you would also have to declare and define these two variables beforehand. Like thisHowever 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 instanceoutput
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 thisNote the additional parentheses at the end of
$error->{message}()
to call the reference to be called instead of evaluatedoutput