Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: tie() weirdness

by blazar (Canon)
on Nov 21, 2005 at 11:50 UTC ( #510421=note: print w/replies, xml ) Need Help??


in reply to Re: tie() weirdness
in thread tie() weirdness

But from that thread seems to refer to a (possible) bug in tied arrays, whereas I found a (possibly) anomalous behaviour with tied scalars.

However, this particular post from that thread brings me to

Both hint at the fact that tied vars may... well, evaluate twice under some circumstances. In particular the former seems to refer to a different situation, but the latter portraits exactly the same one as mine. But the same article claims that it was a 5.6.1 specific bug and that it should have been patched - here's the relevant quote:

I searched p5p and it appears to have been reported and patched in development versions --- I just wanted to let people here know so they might avoid some confusion if they run into it. A relevant p5p link (from which the patch is linked) is: here

May it be that the patch never made its way in the next releases or that the same bug has been reintroduced subsequently?

Remark: more importantly, it seems that the FETCH method is not called twice:

#!/usr/bin/perl -l use strict; use warnings; sub TIESCALAR { bless \my $i, shift } sub FETCH { warn "FETCH called!\n"; ${shift,}++ } tie my $s, 'main'; print "$s$s$s"; __END__
Output:
FETCH called! FETCH called! FETCH called! 112

Replies are listed 'Best First'.
Re^3: tie() weirdness
by Animator (Hermit) on Nov 21, 2005 at 12:16 UTC

    It certainly is a strange thing. This post lists some more strange things and no solution. Something certainly was changed (between v5.6 and v5.8)... what I don't know.

    If you run your latest code on v5.6.1 then you'll see that FETCH is called 4 times, not three times. (and it prints 123).

    If you change it into print $s . $s . $s; then you get 123 on v5.6, and 112 on v5.8.

    Change it into: print "" . $s . $s . $s; and you get 012. (on v5.6 and v5.8)

    Change it into: print "$s$s" . "$s$s" . "$s$s" and you get 124578 on v5.6 and 113355 on v5.8.

    There is another case in which the scalar is evaluated twice (and that I didn't immediatly see in here/the links posted here): hash keys on v5.6. If the key does not exist then it will re-evaluate the scalar. And the last value will be used for the key. This seems to be fixed in v5.8.

    If this was 'normal' code I would strongly advice against using such a construct, since there are too many cases where it can go wrong...

    And if you look at the perlapi then you will find function that evaluate it twice aswell... (for example SvIV and SvIVx)

    Update: Some other 'strange' cases:

    • print $s . "$s$s" : v5.6: FETCH is called 5 times and the output is 412. v5.8: FETCH called 3 times and the output: 211.
    • print $s . "$s" . $s: v5.6: output 203. v5.8 output: 102. (The output of print $s.$s.$s is mentioned above)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2022-12-08 02:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?