| 1 | package FormValidator::Simple::Plugin::Math; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use FormValidator::Simple::Constants; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Math::Expression; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | 1 | 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<x> is the value to be passed. e.g. C<x**3> | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head2 is_what | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | Sets some number. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | Switches the true-false evaluation if C<is_what> starts with C<!>. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub MATH{ | ||||||||||||||||||||||||||
|
Subroutine
MATH: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 0 | my ($self, $params, $args) = @_; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | my $calc = $args->[0] || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->[0] || 0
MC/DC0%0/1
$args->[0] || 0
$args->[0]
Truth table0%0/2
A
A= $args->[0]
| |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my $equals = $args->[1] || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->[1] || 0
MC/DC0%0/1
$args->[1] || 0
$args->[1]
Truth table0%0/2
A
A= $args->[1]
| |||||||||||||||||||||||||||||
| 51 | 0 | my $data = $params->[0]; | |||||||||||||||||||||||||||
| 52 | 0 | my $is = TRUE; | |||||||||||||||||||||||||||
| 53 | 0 | my $is_not = FALSE; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($equals =~ /^\!(\d+?)$/){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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 | ▶ | if ($equals =~ /[^\d]/ || $data =~ /[^\d]/){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$equals =~ /[^\d]/ or $data =~ /[^\d]/
MC/DC0%0/2
$equals =~ /[^\d]/ or $data =~ /[^\d]/
$equals =~ /[^\d]/
$data =~ /[^\d]/
Truth table0%0/3
A or B
A= $equals =~ /[^\d]/B= $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 | ▶ | return ($value == $equals) ? $is : $is_not; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Yusuke Sugiyama, C<< <ally at blinkingstar.net> >> | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head1 BUGS | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Please report any bugs or feature requests to | ||||||||||||||||||||||||||||
| 81 | C<bug-formvalidator-simple-plugin-math at rt.cpan.org>, or through the web interface at | ||||||||||||||||||||||||||||
| 82 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FormValidator-Simple-Plugin-Math>. | ||||||||||||||||||||||||||||
| 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<http://annocpan.org/dist/FormValidator-Simple-Plugin-Math> | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | L<http://cpanratings.perl.org/d/FormValidator-Simple-Plugin-Math> | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FormValidator-Simple-Plugin-Math> | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | L<http://search.cpan.org/dist/FormValidator-Simple-Plugin-Math> | ||||||||||||||||||||||||||||
| 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 | 1; # End of FormValidator::Simple::Plugin::Math | |||||||||||||||||||||||||||