Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Numification of strings

by morgon (Deacon)
on Aug 02, 2010 at 18:01 UTC ( #852509=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Numification of strings
in thread Numification of strings

in Perl, everything is a string until used in a numeric context

I don't think that is true.

In your example you put quotation-marks around the value, so little surprise that you end up with a string...

Consider this:

use Devel::Peek; my $a = "1"; my $b = 2; print Dump($a); print Dump($b);
This produces:

SV = PV(0x98c9700) at 0x98ea3f8 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x98e6368 "1"\0 CUR = 1 LEN = 4 SV = IV(0x98ea474) at 0x98ea478 REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 2
As you can see $a is a string "PV", while $b is an int "IV", even thought it was never used in an numeric context.


Comment on Re^3: Numification of strings
Select or Download Code
Re^4: Numification of strings
by Marshall (Prior) on Aug 02, 2010 at 18:11 UTC
    I updated my post above:
    re: Also of note is that in Perl, a string that looks like a number is still a string until used in a numeric context. Feel free to suggest alternate wording for this sentence.

    These leading zero situations can happen when reading DB files of various formats, say from a split() or whatever.

Re^4: Numification of strings
by ww (Bishop) on Aug 02, 2010 at 18:27 UTC
    "As you can see $a is a string "PV", while $b is an int "IV", even thought it was never used in an numeric context."

    If you mean what I think you mean -- that $b is an int BECAUSE it was never used in a numeric content -- I think you're wrong, morgon: $b IS an IV, because it was instantiated that way... as an integer, without quotes that would have made it a string.

    Update: Maybe I have the code tags right... at last.

      If you mean what I think you mean ...

      :-)

      I meant to disprove the theory that a value becomes an int only after being used in a numeric context by showing an example in which a value is only instantiated (and never used in a numeric context), yet still is numeric.

      Or less convoluted: I probably meant what you said.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2015-07-07 13:36 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