| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package FormValidator::Simple::Plugin::Math; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 21335 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 4 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 5 | 1 |  |  | 1 |  | 893 | use FormValidator::Simple::Constants; | 
|  | 1 |  |  |  |  | 209 |  | 
|  | 1 |  |  |  |  | 63 |  | 
| 6 | 1 |  |  | 1 |  | 833 | use Math::Expression; | 
|  | 1 |  |  |  |  | 12735 |  | 
|  | 1 |  |  |  |  | 414 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | =head1 NAME | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | FormValidator::Simple::Plugin::Math - Math evaluation for FormValidator::Simple | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | =head1 VERSION | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | Version 0.03 | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =cut | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | You can evalute the form data with math expression. | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | use FormValidator::Simple qw/Math/; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | my $result = FormValidator::Simple->check( $req => [ | 
| 27 |  |  |  |  |  |  | category => [ 'NOT_BLANK', ['MATH', 'x % 100', '!0']], | 
| 28 |  |  |  |  |  |  | ### valid if category % 100 != 0 | 
| 29 |  |  |  |  |  |  | ]); | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | =head1 EVALUATION | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | ['MATH', some_math_expression, is_what] | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | =head2 some_math_expression | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | C is the value to be passed. e.g. C | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | =head2 is_what | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | Sets some number. | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | Switches the true-false evaluation if C starts with C. | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | =cut | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub MATH{ | 
| 48 | 0 |  |  | 0 | 0 |  | my ($self, $params, $args) = @_; | 
| 49 | 0 |  | 0 |  |  |  | my $calc = $args->[0] || 0; | 
| 50 | 0 |  | 0 |  |  |  | my $equals = $args->[1] || 0; | 
| 51 | 0 |  |  |  |  |  | my $data = $params->[0]; | 
| 52 | 0 |  |  |  |  |  | my $is     = TRUE; | 
| 53 | 0 |  |  |  |  |  | my $is_not = FALSE; | 
| 54 | 0 | 0 |  |  |  |  | if ($equals =~ /^\!(\d+?)$/){ | 
|  |  | 0 |  |  |  |  |  | 
| 55 | 0 |  |  |  |  |  | $equals = $1; | 
| 56 | 0 |  |  |  |  |  | $is     = FALSE; | 
| 57 | 0 |  |  |  |  |  | $is_not = TRUE; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  | elsif ($equals eq '!'){ | 
| 60 | 0 |  |  |  |  |  | $equals = 0; | 
| 61 | 0 |  |  |  |  |  | $is     = FALSE; | 
| 62 | 0 |  |  |  |  |  | $is_not = TRUE; | 
| 63 |  |  |  |  |  |  | } | 
| 64 | 0 | 0 | 0 |  |  |  | if ($equals =~ /[^\d]/ || $data =~ /[^\d]/){ | 
| 65 | 0 |  |  |  |  |  | return TRUE; | 
| 66 |  |  |  |  |  |  | } | 
| 67 | 0 |  |  |  |  |  | my $m = new Math::Expression; | 
| 68 | 0 |  |  |  |  |  | $m->VarSetScalar('x',$data); | 
| 69 | 0 |  |  |  |  |  | my $mtree = $m->Parse($calc); | 
| 70 | 0 |  |  |  |  |  | my $value = $m->EvalToScalar($mtree); | 
| 71 | 0 | 0 |  |  |  |  | return ($value == $equals) ? $is : $is_not; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =head1 AUTHOR | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | Yusuke Sugiyama, C<<  >> | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | =head1 BUGS | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | Please report any bugs or feature requests to | 
| 81 |  |  |  |  |  |  | C, or through the web interface at | 
| 82 |  |  |  |  |  |  | L. | 
| 83 |  |  |  |  |  |  | I will be notified, and then you'll automatically be notified of progress on | 
| 84 |  |  |  |  |  |  | your bug as I make changes. | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | =head1 SUPPORT | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | perldoc FormValidator::Simple::Plugin::Math | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | You can also look for information at: | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =over 4 | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | L | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | L | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =item * RT: CPAN's request tracker | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | L | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | =item * Search CPAN | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | L | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | =back | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =head1 ACKNOWLEDGEMENTS | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | =head1 COPYRIGHT & LICENSE | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | Copyright 2007 Yusuke Sugiyama, all rights reserved. | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 121 |  |  |  |  |  |  | under the same terms as Perl itself. | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =cut | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | 1; # End of FormValidator::Simple::Plugin::Math |