perlquestion
footpad
<P><I>The apprentice, after taking aspirin for the pain in his forehead, finally caves after several hours...</I></P>
<P>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:</P>
<BLOCKQUOTE><I>Best perlop check? Try this: <code>eval {local $^W = 1; $num + 0}; # Now check $@</code></I></blockquote>
<P>Okay. So I'm trying to suit the action to these words and am not having much success:</P>
<code>#!/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;</code>
<P>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. </P>
<P>My petitions are:</P>
<UL><LI><P>Have I implemented the mysterious monk's advice properly? (Probably not; gentle correction encouraged).</P></LI>
<LI><P>Why are "Shoe" and "Fred" being recognized as numbers? Try it out; you'll see.</P></LI>
<LI><P>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")?</P></LI>
</UL>
<P>Yes, I am aware of the [isbn://1565922433|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.</P>
<P>Many thanks in advance...</P>
<P><I>--f</I></P>