in reply to How to pass a variable string as conditional argument to if statment
use eval
if (eval $var) {
Note: you can also clean up your code a little bit by using q and qq in places where you want to include single and double quotes in strings.
#!/usr/bin/perl -w use strict; use warnings; my $hash_ref1 = { this => 'blarney', some => 'foo', chancy => 'at best', }; my $hash_ref2 = { this => 'that', some => 'thing', another => 'something', }; print "this is $$hash_ref2{'this'}\n"; my @a = ($hash_ref1, $hash_ref2); my $var = q{$$hash_ref{'this'} eq 'that'}; foreach my $hash_ref (@a) { if ($$hash_ref{'this'} eq 'that') { print qq{I only want to see case where: "this really does equa +l $$hash_ref{'this'}"\n}; } if (eval $var) { print "this really does equal $$hash_ref{'this'}\n\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to pass a variable string as conditional argument to if statment
by sgrey (Novice) on May 17, 2011 at 20:42 UTC | |
by wind (Priest) on May 17, 2011 at 21:07 UTC | |
by Anonymous Monk on Feb 05, 2016 at 20:32 UTC | |
by poj (Abbot) on Feb 06, 2016 at 07:00 UTC | |
by choroba (Cardinal) on Feb 06, 2016 at 08:25 UTC |