View Single Post
  #4  
Old 12-13-2012, 04:37 PM
c0ncrete's Avatar
c0ncrete
Dragon
 
Join Date: Dec 2009
Posts: 719
Default

valid point.

Code:
sub IsFlagged
{
    my %flag = @_;
    return 0 unless %qglobals;
    while (my ($key, $val) = each %flag) {
        if (ref $val eq 'HASH') {
            my $op = $key;
            unless ($op ~~ ['==', '>=', '>', '<=', '<', '!=']) {
                warn "invalid operator ($key) found in plugin::IsFlagged()";
                warn "skipped ".keys(%$val)." check(s)";
                next;
            }
            while (my ($key, $val) = each %{$val}) {
                return 0 unless defined $qglobals{$key}
                    and eval "$qglobals{$key} $op $val";
            }
        }
        else {
            return 0 unless defined $qglobals{$key}
                and $qglobals{$key} == $val;
        }
    }
    1;
}
now you can pass an operator as a key with an anonymous hash reference as a value (containing all the key/value pairs you want to check using that operator). if no operator is found (well, it's actually checking for a hash reference as a value), it defaults to ==.

example:
Code:
my $flagged = plugin::IsFlagged('>=' => {bic_quin => 4}, bic_fer => 11, bic_riw => 10);
would make sure $qglobals{bic_quin} was greater than or equal to 4, and that $qglobals{bic_fer} was equal to 11 and $qglobals{bic_riw} was equal to 10.

EDIT:
changed it to where it will only accept numeric comparison operators. that way an accidental assignment won't happen in eval.

Last edited by c0ncrete; 12-13-2012 at 05:16 PM.. Reason: improved example's legiblity and added sanity check.
Reply With Quote