in reply to Re^4: Confirming what we already knew (it's cached)
in thread Confirming what we already knew

Yes. I understood, (and indicated this above?) that when a scalar (that has previously been used in a numeric context) is fetched, the binary version is accessed and used saving the need for a ascii-to-binary conversion.

The statement that diotalevi rightly took me to task for--though I have again attempted to verify this without success--is the one were I suggested that when a scalar that has been used in a numeric context and therefore has a binary version available, is modified numerically, the ascii version is also updated.

Part of what made me think this was the case is that I cannot see any mechanism in the data structures whereby perl would be able to know whether the ascii version needed updating from the binary version.

To clarify (my own thoughts mostly), in the following situation

my $num = '5.1'; ## scalar is a string, NOK is false if ($num == 5.1) { print 'It is'; }

At this point, $num has been used in a numeric context, so a ascii-to-binary conversion has been done, NOK is true, and subsequent references to $num in numeric contexts can re-use the binary value directly avoiding an ftoi().

$num++; ## binary value fetched, incremented and stored $num++; ## binary value fetched, incremented and stored

The question is, did the stringy version of the value get updated when those modification occurred, or does that get delayed until $num is used in a string context?

If the stringy version was not updated, then by what mechanism does perl know that it must do so when, sometime later, I do print $num;?


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

Replies are listed 'Best First'.
Re^6: Confirming what we already knew (it's cached)
by Elian (Parson) on Mar 05, 2003 at 19:59 UTC
    Assigning to a non-tied variable throws out everything that was in the variable, including its flags, and replaces it with the value being assigned.

    So in the $foo++ case, $foo's old value gets thrown out, along with all its flags, and gets a new value and flags to go with it. If $foo used to be a string and num, well, the string part's been tossed out (if it was a numeric increment) or the num part's been tossed (if it was a string increment)

    Things wouldn't work too well if it were otherwise.

Re: Re: Re^4: Confirming what we already knew (it's cached)
by hv (Prior) on Mar 05, 2003 at 21:07 UTC

    When you modify the numeric value (NV) of a scalar, the NOK flag is set and the other flags (IOK, POK) are cleared: that means if we need the NV, we can just grab it (since NOK is true), but if we need the string value (PV, for 'pointer') we need to do a conversion.

    When the conversion is done, we also need to distinguish whether the conversion was lossy or not: for example if we use a numeric 1.2 in an integer context, we cache the integer value 1 and set pIOK ('private' integer OK) to avoid the need to recalculate the integer next time we need it, but if it were 1.0 we'd also set IOK.

    The Devel::Peek module is very useful for seeing this sort of thing:

    perl -MDevel::Peek -we '$a=1.2;Dump($a);$b="$a";Dump($a)'

    Hugo

      Agreed. Though it does take a fair amount of reading to understand the output I find. diotalevi put me on to Perl Guts Illustrated, which has been a great help as I always have a pile of scraps of paper next to me with sketches and tables scribbled all over them when I am programming. Despite the fact that I have no aptitude artistically, I seem to have a visually oriented brain.

      However, I'm a little confused by what your example was illustrating?

      C:\test>perl -MDevel::Peek -we "$a=1.2;Dump($a);$b="$a";Dump($a)" Name "main::b" used only once: possible typo at -e line 1. SV = NV(0x1bc58c0) at 0x1bd2a50 REFCNT = 1 FLAGS = (NOK,pNOK) NV = 1.2 SV = NV(0x1bc58c0) at 0x1bd2a50 REFCNT = 1 FLAGS = (NOK,pNOK) NV = 1.2

      Examine what is said, not who speaks.
      1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
      2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
      3) Any sufficiently advanced technology is indistinguishable from magic.
      Arthur C. Clarke.

        If you change the quotes, you need to be careful of the interior; try:

        perl -MDevel::Peek -we "$a=1.2;Dump($a);$b=qq{$a};Dump($a)"

        Hugo
Re^6: Confirming what we already knew (oops)
by tye (Sage) on Mar 06, 2003 at 07:10 UTC

    Sorry, you did in fact say that. I probably should have just waited for someone else to respond. I didn't take the time to correctly understand your point. I was correct in understanding that you thought more conversions were taking place than really do, but that was about it.

    But that was just sloppy. Again, sorry.

                    - tye