develooper Front page | perl.perl5.porters | Postings from April 2010

Double magic with $x = $a[0] or $x = $h{key}

Thread Next
Vincent Pit
April 25, 2010 02:28
Double magic with $x = $a[0] or $x = $h{key}
Message ID:
The following code

     use 5.012;
     use Tie::Scalar;
      package X;
      use base qw/Tie::StdScalar/;
      sub FETCH {
       say "fetch";
       goto $_[0]->can("SUPER::FETCH")
     my @a = ("banana");
     tie $a[0], "X";
     my $x = $a[0];

prints "fetch" twice with 5.13.0 and only once with 5.12.0. This change 
in behaviour appeared recently with commit 
fd69380d5d5b95ef16e2521cf4251b34ee0ce151. The magic is first called 
inside aelemfast/aelem/helem with the mg_get() that was added as the 
fix, and then during the scalar assignment. This is a pure Perl example, 
but all kinds of get magic attached to an array or hash element now 
triggers twice with this construct, with the exception of elements of 
tied arrays/hashs which recieved a special treatment. This is the reason 
for Variable::Magic's testsuite failures on 5.13.0, although this isn't 
critical to its functionality.

I'm unsure if this is a bug (there's no guarantee made on how many times 
magic callbacks can be called inside a statement), but if the solution 
is simple I'd rather see it fixed in the core.


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About