Reputation: 1693
I have a selection of statements like so:
my $name = $criteria->{'name'} if $criteria->{'name'};
my $age = $criteria->{'age'} if $criteria->{'age'};
my $dob = $criteria->{'dob'} if $criteria->{'dob'};
.
.
.
# there are around 8 of these
And I would like to do something similar to:
foreach (qw/name age dob .../) {
${$_} = $criteria->{$_} if $criteria->{$_};
}
I'm sure I've done this before, but when I tried it using use strict
it threw an error.
Is there a way to do this? Or is there a better way to do what I want? I'm just being lazy really, but if there is a way to do it, it would be interesting to see.
If it is possible to do, would it be more efficient to run memory-wise? I'm trying to improve the speed of my script.
Upvotes: 2
Views: 94
Reputation: 240473
The better way is to use the hash you already have, instead of using the current package namespace as a hash. And no, you're not saving memory or time with what you're trying to do. In fact you're using more of both.
Upvotes: 8