Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: variable type

by imp (Priest)
on Dec 07, 2006 at 14:55 UTC ( #588348=note: print w/ replies, xml ) Need Help??


in reply to Re: variable type
in thread variable type

That's almost correct. $ will match an optional newline as well, so both "5" and "5\n" are valid. Better to use \z

print "1. number\n" if "5" =~ /^\d+$/; print "1. number and newline\n" if "5\n" =~ /^\d+$/; print "2. number\n" if "5" =~ /^\d+\z/; print "2. number and newline\n" if "5\n" =~ /^\d+\z/
Update
The advice to use a \z fixes the problem of bad data being submitted, but it might block otherwise good data, depending on the input source. Data read from a filehandle could have the newline present, without it being from a misbehaving user.
chomping the field in question is a better approach than being stricter with the regex.


Comment on Re^2: variable type
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://588348]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-12-25 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls