sgrey has asked for the wisdom of the Perl Monks concerning the following question:

How do I get the if statement to evaluate the conditional I have placed into $var?

#!/usr/bin/perl -w use strict; my $hash_ref2={ this=>'that', some=>'thing', another=>'something', }; my $hash_ref1={ this=>'blarney', some=>'foo', chancy=>'at best', }; my @a=($hash_ref1,$hash_ref2); print "this is ".$$hash_ref2{'this'}."\n"; my $var='$$hash_ref{\'this\'} eq \'that\''; foreach my $hash_ref(@a){ if($$hash_ref{'this'} eq 'that'){ print "I only want to see case where: \"this really does equal " +.$$hash_ref{'this'}."\"\n"; } if("$var"){ print "this really does equal ".$$hash_ref{'this'}."\n\n"; } }
Results:
this is that
this really does equal blarney

I only want to see case where: "this really does equal that"
this really does equal that
  • Comment on How to pass a variable string as conditional argument to if statment
  • Download Code

Replies are listed 'Best First'.
Re: How to pass a variable string as conditional argument to if statment
by ikegami (Patriarch) on May 17, 2011 at 17:23 UTC
    Does it really have to be a string?
    my $var = sub { $_[0]->{this} eq 'that' }; foreach my $hash_ref (@a) { if ($hash_ref->{this} eq 'that') { ... } if ($var->($hash_ref)) { ... } }
Re: How to pass a variable string as conditional argument to if statment
by wind (Priest) on May 17, 2011 at 16:58 UTC

    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"; } }
      Thanks for the 'eval' redirect.  Did the trick, and since now that I get it, will appear alot more in my code. 
      Much Obliged!!!
      

        I'd actually recommend using anonymous subroutines much more than eval for something like this. Would change your code to the following:

        #!/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 $test = sub {$_->{this} eq 'that'}; foreach (@a) { if ($_->{this} eq 'that') { print qq{I only want to see case where: "this really does equa +l $_->{this}"\n}; } if ($test->()) { print "this really does equal $_->{this}\n\n"; } }

        Or if you don't want to rely on the $_ variable, you can pass the variable as a parameter to the sub:

        my $test = sub {$_[0]->{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 ($test->($hash_ref)) { print "this really does equal $hash_ref->{this}\n\n"; } }