develooper Front page | perl.perl5.porters | Postings from February 2003

Re: odd (or not so odd?) segmentation fault in 5.8.0

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
February 17, 2003 11:27
Subject:
Re: odd (or not so odd?) segmentation fault in 5.8.0
Message ID:
20030217192357.A14740@fdgroup.com
On Mon, Feb 17, 2003 at 07:11:50PM +0200, Enache Adrian wrote:
> On Mon, Feb 17, 2003 at 11:15:57AM +0000, Dave Mitchell wrote:
> > This is why 'untie $a', and '$a' have no magical effect within
> > FETCH().
> 
> If this was true, all my previous my examples should print just
> (1) (1) (1) (1) (1) (1)
> Am I wrong ?

yes - see below

> Try this too:
> 
> $ perl5.8.0 -e '$,=" ";$a = 1; { package P; sub TIESCALAR {bless[]}
> sub FETCH {$main::a+1} } tie $a, P; print $a,"$a $a $a $a",$a,"\n"'
> 6 2 3 4 5 7

What happens is as follows:

    sub P::FETCH { $a+1 }
    $a = 1;
    tie $a, P;
    $x = $a;

is executed as follows:

1) the plain SV associated with $a gets the value 1
2) The $a gets magic attached to it
3) an attempt is made to get the value of $a
  3a) $a's magic is temporarily disabled, then FETCH is called
  3b) FETCH accesses the current value of (plain) $a, which is 1, and
      returns that plus 1
  3c) the returned value from FETCH is assigned to $a's SV, ie SvNVX($a) = 2
4) $a's current value (2) is assigned to $x

The next time round the loop, $a's plain value is now 2.

Whether this is the correct or best possible behaviour is another matter -
personally I think it is reasonable given the complexities of tie.

-- 
In the 70's we wore flares because we didn't know any better.
What possible excuse does the current generation have?

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About