$allowed{action} = 'post|edit|delete|long|short'; $query->{action} eq "post" ? (($allowed{msgid} = '^(\d+|new)$'), ($allowed{data} = '^(([^|+]*[^|]+.*\|){4}[^|+]*[^|]+.*)$')) : 1; $query->{action} eq "edit" ? ($allowed{msgid} = '^(\d+|new)$') : 1; $query->{action} eq "delete" ? ($allowed{msgid} = '^(\d+)$') : 1; $query->{action} eq "long" ? (($allowed{sort} = '^(author|date|subject|expire)$'), ($allowed{msgid} = '^(\d*|all)$')) : 1; $query->{action} eq "short" ? (($allowed{sort} = '^(author|date|subject|expire)$'), ($allowed{msgid} = '^(\d+|all)$')) : 1; foreach $key (keys %$query){ exists $allowed{$key} ? 1 : return 0; return 0 if $query->{$key}!~/$allowed{$key}/; 1; }
Now, that said, here's another way to handle this:
Notes:# Map keys to coderefs (used sort of like a switch statement) my %ActionMap = (post => sub{ $allowed{msgid} = '^(\d+|new)$'; $allowed{data} = '^(([^|+]*[^|]+.*\|){4}[^|+]*[^|]+.*)$'; }, edit => sub{ $allowed{msgid} = '^(\d+|new)$'; }, delete => sub{ $allowed{msgid} = '^(\d+)$'; }, long => sub{ $allowed{sort} = '^(author|date|subject|expire)$'; $allowed{msgid} = '^(\d*|all)$'; }, short => sub{ $allowed{sort} = '^(author|date|subject|expire)$'; $allowed{msgid} = '^(\d+|all)$'; }); # "Call" the coderef matching the "switch" value $ActionMap{$query->{action}}->(); for my $key (keys %$query){ return 0 unless exists $allowed{$key} && $query->{$key} =~ /$allowed +{$key}/; }
Russ
Brainbench 'Most Valuable Professional' for Perl
In reply to RE: rule base (comma operator / 'switch' construct)
by Russ
in thread How to write a nice rule base?
by TGI
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |