trouble with simple expression

A

alexjaquet

Hi,

I could not figure why my expression doesn't work

I want to accept only digit sample code I use :


local our $quantity = $query->param ("quantity"); #parameter = asdfasdf
if (!$quantity =~ /^[0-9]/) {
print "Content-Type: text/html\n";
local our $url = $ENV{'HTTP_REFERER'};
$url = $url."&error=$SERVER{'not_decimal'}";
print "Location: $url\n\n";
}else {
print "Content-Type: text/html\n\n";
print "quantity : $quantity\n";
}

the problem I got is I always do the else part :(
 
B

Brian Wakem

Hi,

I could not figure why my expression doesn't work

I want to accept only digit sample code I use :


local our $quantity = $query->param ("quantity"); #parameter = asdfasdf
if (!$quantity =~ /^[0-9]/) {


That should be:

if ($quantity !~ /^[0-9]/) {

But that still isn't any good as it will accept a quantity of 2B, so you
need:

if ($quantity !~ /^[0-9]+$/) {

or

if ($quantity !~ /^\d+$/) {
 
A

alexjaquet

Brian said:
Hi,

I could not figure why my expression doesn't work

I want to accept only digit sample code I use :


local our $quantity = $query->param ("quantity"); #parameter = asdfasdf
if (!$quantity =~ /^[0-9]/) {


That should be:

if ($quantity !~ /^[0-9]/) {

But that still isn't any good as it will accept a quantity of 2B, so you
need:

if ($quantity !~ /^[0-9]+$/) {

or

if ($quantity !~ /^\d+$/) {

Thanks Brian, have a nice week end :)
 
T

Tad McClellan

local our $quantity = $query->param ("quantity"); #parameter = asdfasdf
local our $url = $ENV{'HTTP_REFERER'};


You should always prefer lexical variables over package variables,
except when you can't.

Why do you think that you cannot use lexical variables there?
 
B

Brian McCauley

Tad said:
You should always prefer lexical variables over package variables,
except when you can't.

Why do you think that you cannot use lexical variables there?

Could be he's got variables that are shared between the file scope and
subroutines in a CGI script that's to run under Apache+mod_perl.

But it seems more likely that there's no reason here and he should be
using lexicals.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top