So, on to your question
If you're using regexen, to accept, for example, only the digital portion of '$123.45' your might want to "validate" by (grossly oversimplified)
prints: 123.45use strict; use warn; my $input = "\$123.45"; if ( $input =~ /\D*(\d{0,3}\.\d{0,2}).*/ ) { my $valid = $1; print $valid; } else { print "not valid\n"; }
The regex above (sequentially) looks for (and if found, discards) any leading non-digit character(s), then for 0 to 3 digits, a literal dot and 0 to 2 more digits, capturing the digits and the dot.
For dates, you'd be well-advised to convert the user's input to some standard form, and test that.
And, genericly, you may wish to read about untainting [ Question about untainting data, Untainted done right! (esp tye's reply] and similar nodes found with search or supersearch} because much of what's discussed will be applicable to your data validation needs.
BTW: the notion of ...a variable that is defined as an integer.... may be tripping you up if you're accustomed to a strongly typed language. Perl isn't. OTOH, if you mean that YOU want that $var to be an integer, see the likes of numify....
Update: minor formatting, closed <tt> tag
In reply to Re: perl typecasting
by ww
in thread perl typecasting
by perl_junkie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |