The apprentice, after taking aspirin for the pain in his forehead, finally caves after several hours...
Some time ago, a certain monk (privately) advised the following after seeing my poor attempts to verify that a certain CGI parameter was actually a number, e.g. a non-blank, Base-10 numeric value that may (or may not) contain decimal points and/or hyphens masquerading as negative number indicators:
Best perlop check? Try this: eval {local $^W = 1; $num + 0}; # Now check $@
Okay. So I'm trying to suit the action to these words and am not having much success:
#!/usr/bin/perl -wT
use strict;
$|++;
use CGI qw( :standard );
my $result = '';
my %params = param();
if ( my $error = cgi_error() )
{
print header( -status => $error );
exit 0;
}
my $invalue = param( 'INVALUE' );
if ( defined $invalue )
{
eval{ local $^W = 1; $invalue + 0 }; ### Is Number? (Line 19)
if ( $@ )
{ $result = "$invalue is *not* a Number; Details: $@" }
else
{ $result = "$invalue appears to be a number."; }
}
else
{ $result = "Enter a value to test." }
print header( "text/html" ),
start_html('Test'),
h1('Validation'),
start_form(),
p( $result ),
p( "Please enter a number to test: ", textfield( 'INVALUE', '' )
+ ),
p, submit,
end_form, hr,"\n";
print end_html;
exit 1;
As the more experienced of you will recognize, this is resulting in values such as "20" as being recognized as a number. Sadly, so are "Shoe", "Fred" and other patently non-numeric values, by which (just to be perfectly clear to the most rententive) are *not* numeric in the locale, idiom, and operational parameters that I'm trying to communicate in.
My petitions are:
Have I implemented the mysterious monk's advice properly? (Probably not; gentle correction encouraged).
Why are "Shoe" and "Fred" being recognized as numbers? Try it out; you'll see.
How do I get rid of the "Useless use of addition in void context at scriptname.cgi line 19." error that's appearing when I run this from the command-line? Perhaps more appropriately, how do I do so and achieve my primary goal, e.g. confirming that the user of my CGI form has indeed entered a non-blank number (including "0")?
Yes, I am aware of the Cookbook and its examples; you'll note this monk had a different approach in mind. I'm trying to get it to work. A gentle nudge would be greatly appreciated.
Many thanks in advance...
--f
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.