I want to create a hash of optional query parameters that are sometimes passed to my subroutine. Sometimes a query parameter called welcome
is passed in, and the value is either 1 or 0.
If that variable exists, I want to add it to a hash.
I've created a configuration value called OPTIONAL_URL_PARAMS which is a list of expected parameter names that can be passed in:
use constant OPTIONAL_URL_PARAMS => ("welcome")
So far I have:
my $tempParams = {};
if ( $optionalParam ) {
foreach my $param (@{&OPTIONAL_URL_PARAMS}) {
if ($optionalParam eq $self->{$param}) {
$tempParams->{$param} = $optionalParam;
$tempParams->{$param} =~ s/\s+//g; # strip whitespace
}
}
}
But this tries to use the value of $self->{$param}
instead of its name. i.e. I want welcome to match welcome
, but it's trying to match 1 instead.
I know when it comes to keys in a hash you can use keys %hash
, but is there a way you can do this with regular variables?
Edit
My subroutine is being called indirectly:
my $url_handler = URL::Handler->new($param, $optionalParam);
sub new {
my $class = shift;
my $args = @_;
my $self = {
param => $args{param},
optionalParams => $args{optionalParam}
};
}
If $optionalParam
's variable name is 'welcome', then I want to try and map it to the constant welcome
.