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

Re: Numification of strings

by biohisham (Priest)
on Aug 02, 2010 at 06:10 UTC ( #852410=note: print w/ replies, xml ) Need Help??


in reply to Numification of strings

While a variable is a string and if it has neither been assigned a value nor numified its value will be '', I am not sure on this but I think '' or undef or 0 can be interchanging depending on the context that the null variable scalar is used under (i.e when a scalar which's null is numified its value becomes 0).

Isn't this behaviour mainly hiding ugly bugs?
This behavior seems consistent and indeed in/of use, for instance, consider when quickly wanting to start a counter:

C:\Documents and Settings>perl - print "$hash{count} \n"; print -$hash{count},"\n"; print --$hash{count},"\n"; print ++$hash{count},"\n"; __END__ 0 -1 0 C:\Documents and Settings>

Update: did s/' '/''/, thanks Jenda for noticing the unintended typo as '' and ' ' are not the same thing. Added technical corrections too..



Excellence is an Endeavor of Persistence. A Year-Old Monk :D .


Comment on Re: Numification of strings
Select or Download Code
Re^2: Numification of strings
by LanX (Canon) on Aug 02, 2010 at 11:28 UTC
    >This behavior seems consistent and indeed in/of use, for instance, consider when quickly wanting to start a counter:

    Sure that's useful, but it's only about converting undef or (maybe sometimes) the empty string ''.

    Cheers Rolf

      "but it's only about converting undef"

      Lest you or anyone else read that as generally true, I repeat, for emphasis re warnings (or for brevity here, -w) and re 'consistent':

      perl -we "$x="LanX"; print ($x * 3);" Argument "LanX" isn't numeric in multiplication (*) at -e line 1. 0 perl -we "$x="LanX"; print ($x % 3);" Argument "LanX" isn't numeric in modulus (%) at -e line 1. 0 perl -we "$x="LanX"; print (3^$x);" Argument "LanX" isn't numeric in bitwise xor (^) at -e line 1. 3 perl -we "$x="LanX"; print (3**$x);" Argument "LanX" isn't numeric in exponentiation (**) at -e line 1. 1

      but

      perl -we "$x='7LanX'; print (3^$x);" Argument "7LanX" isn't numeric in bitwise xor (^) at -e line 1. 4 perl -we "$x='7LanX'; print ($x*3);" Argument "7LanX" isn't numeric in multiplication (*) at -e line 1. 21 perl -we "$x='7LanX'; print (3**$x);" Argument "7LanX" isn't numeric in exponentiation (**) at -e line 1. 2187

      and where it's useful -- using a string as a number because it's being used in an operation which is arithmetic:

      perl -we "$x=\"5\"; print ($x*3);" # 2 15 perl -we "$x='5'; print ($x*3);" 15

      2   Interesting (or not enough coffee yet?): doze balked at doublequotes without escapes here, but passed 'em cheerfully above. WTF?

        > > "but it's only about converting undef"

        converting undef is differently handled with ++, enabling warnings doesn't help here!

        see Re^3: Numification of strings

        Cheers Rolf

      wow incrementing with ++ is not orthogonal to +1!

      DB<33> my $y;$y++ DB<34> my $y;$y+1 Use of uninitialized value $y in addition (+) at (eval 38)[/usr/share/ +perl/5.10/perl5db.pl:638] line 2.

      from perlop

      The auto-increment operator has a little extra builtin magic t +o it. ...(<i>special magic for incrementing strings, ie <c>++($x='x9') eq 'y +0'</i>)... "undef" is always treated as numeric, and in particular is chan +ged to 0 before incrementing (so that a post-increment of an undef value + will return 0 rather than "undef").

      Cheers Rolf

        Orthogonal means unrelated, independent. For example, if the size of a fish is orthogonal to its coloring, knowing the colour of a fish sheds no information about its size and vice-versa.
Re^2: Numification of strings
by Jenda (Abbot) on Aug 02, 2010 at 13:32 UTC

    No, it will not be ' ', it will be ''. That's not the same thing.

    And actually if a variable has not been assigned, then it's value is undef, which is not a string, but its stringified value is '' an numified value is 0. There is no such thing as an "undefined string" or "string variable"! It is a scalar variable whose value may be undef and/or string and/or integer and/or float and/or reference.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      well always? :)

      dualvar NUM, STRING Returns a scalar that has the value NUM in a numeric contex +t and the value STRING in a string context. $foo = dualvar 10, "Hello"; $num = $foo + 2; # 12 $str = $foo . " world"; # Hello world

      Cheers Rolf

        And what does this disagree with? I never said the integer and string values of a scalar variable have to be in any way related.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-09-22 11:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (189 votes), past polls