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

So this is what I have..
$min = some number if defined $max = some number if defined $Files = some number $IdleTime = some number if (defined($max)) { $if = "($Files >= $max)"; } if (defined($min)) { if ($if) { $if .= " || ($IdleTime >= $min)"; } else { $if .= "($IdleTime >= $min)"; } }
I want to build the if statement and at the time of execution, have something like
if ({$if}) { print "hi"; } else { print "hello"; }
I know this can be done ('cause I did it some years ago & I've grown old & senile)

...any help would be greatly appreciated...thanx...

Replies are listed 'Best First'.
Re: a not so complex if statment
by ikegami (Patriarch) on Jun 28, 2005 at 21:39 UTC

    You need to compile the Perl code in $if, which is what eval EXPR does.

    if (eval $if) { print "hi"; } else { print "hello"; }

    But in the example you've shown, you're better off just storing a boolean value in $if:

    $min = some number if defined $max = some number if defined $Files = some number $IdleTime = some number $if = 0; $if = ($Files >= $max) if defined $max; $if ||= ($IdleTime >= $min) if defined $min; if ($if) { print "hi"; } else { print "hello"; }

    Update: When using the eval method, you probably want to use single quotes when building up $if. Let eval do the interpolation.

Re: a not so complex if statment
by GrandFather (Saint) on Jun 29, 2005 at 03:44 UTC

    The match logic can be made clearer and easier to debug like this:

    { next if ! defined ($max) and ! defined ($min); next if ! defined ($min) and $Files < $max; next if ! defined ($max) and $IdleTime < $min; next if $Files < $max and $IdleTime < $min; print "Match\n"; last; } continue { print "No match\n"; }

    Perl is Huffman encoded by design.
Re: a not so complex if statment
by ambrus (Abbot) on Jun 29, 2005 at 08:16 UTC

    Use procedures. Here's a hypothetic example:

    if (defined($max)) { $if = sub { ($Files >= $max) }; } if (defined($min)) { if ($if) { my $oldif = $if; $if = sub { &$oldif() || ($IdleTime >= $min) +}; } else { $if = sub { ($IdleTime >= $min) }; } } # ... # then you invoke it with if (&$if()) { print "hi"; } else { print "hello"; }

    And you may want to get a copy of MJD's HOP. I don't really know if it helps here, as I haven't yet got that book, but I think it may.

    Update: don't belive what ikegami said above, eval-string is evil.

    Update 2: but he's right about the boolean values, of course.

Re: a not so complex if statment
by fmerges (Chaplain) on Jun 28, 2005 at 22:09 UTC

    Hi,

    If you'll use the $max and $min for presentation later you could initialize it with my $min = '';

    So that you can still use it for comparisions because when converting into numeric will be 0 and at the same time you don't need to put "if defined" when you print it out, o use the ternary operator to print an alternative empty char, example: &nbsp; if we're talking about a html output...

    Regards,

    :-)
Re: a not so complex if statment
by Anonymous Monk on Jul 01, 2005 at 20:08 UTC
    are you thinking of 'eval' ?