Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Bug or Feature?

by blokhead (Monsignor)
on Feb 22, 2006 at 15:07 UTC ( [id://531982]=note: print w/replies, xml ) Need Help??


in reply to Bug or Feature?

Very interesting, and clever to make a JAPH out of it!

The first time you evaluate ($a & $b), the two arguments are strings ("12" and "8"), so it is the string version of the "&" operator. The result is 0.

But then you do a numerical comparison with $b. So $b's internal scalar caches $b's integer value along with its string value.

Now the second time you evaluate ($a & $b), $b has an integer value inside, so it is treated as an integer (this must the preference of bitwise operators?). By the table below, you can see that as long as one of the arguments to the "&" operator is considered to be an integer, we use the integer/bitwise version of the operator (and the result is 8 this time).

print "12" & "8", $/; # 0 print 12 & 8, $/; # 8 print 12 & "8", $/; # 8 print "12" & 8, $/; # 8

blokhead

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2025-11-18 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (72 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.