Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Handling both string and numeric comparisons

by rob_au (Abbot)
on May 12, 2005 at 11:31 UTC ( #456350=note: print w/ replies, xml ) Need Help??


in reply to Handling both string and numeric comparisons

You can force the numeric context for your string in the following fashion:

if (0 + $param == 5) { ... }
This will execute and provide the comparison operation that you desire without error or warning. Where $param is a string, the result of the inner operation is zero.

 

perl -le "print unpack'N', pack'B32', '00000000000000000000001000000000'"


Comment on Re: Handling both string and numeric comparisons
Select or Download Code
Re^2: Handling both string and numeric comparisons
by Roy Johnson (Monsignor) on May 12, 2005 at 12:01 UTC
    If you use warnings;, you will get a warning:
    Argument "five" isn't numeric in addition (+) at try.pl line 6.
    You will not get a warning for doing string comparison on a number, though.

    Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2015-07-07 12:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (88 votes), past polls