develooper Front page | perl.perl5.porters | Postings from July 2013

[perl #27010] tie doesn't autoviv an aliased hash element

Thread Previous | Thread Next
From:
Father Chrysostomos via RT
Date:
July 16, 2013 07:29
Subject:
[perl #27010] tie doesn't autoviv an aliased hash element
Message ID:
rt-3.6.HEAD-2552-1373959752-1902.27010-15-0@perl.org
On Mon Jul 15 23:28:26 2013, sprout wrote:
> On Mon Feb 23 11:18:23 2004, andrew@pimlott.net wrote:
> > 
> > This is a bug report for perl from andrew@pimlott.net,
> > generated with the help of perlbug 1.34 running under perl v5.8.2.
> > 
> > 
> > -----------------------------------------------------------------
> > [Please enter your report here]
> > 
> > The following amusing function works:
> > 
> >     sub assign { $_[0] = $_[1] }
> >     assign $foo => 1;
> >     assign $foo{bar} => 1;
> > 
> > In particular, in the last line, the hash element is autovivified by
> >    the
> > assignment to the alias $_[0].  The following doesn't:
> > 
> >     sub maitai { tie $_[0] => 'MaiTai' }
> >     maitai $foo;        # ok
> >     maitai $foo{bar};   # not ok
> > 
> > $foo gets tied as expected, but $foo{bar} doesn't.  Evidently,
> >    whatever got
> > tied was dissociated from the hash element.  This can be repaired with
> > 
> >     sub maitai { tie $_[0], 'MaiTai' }
> >     $foo{bar} = undef;
> >     maitai $foo{bar};   # ok
> > 
> > However, this doesn't do it:
> > 
> >     sub maitai { $_[0] = 'DUMMY'; tie $_[0], 'MaiTai' }
> >     maitai $foo{bar};   # $foo{bar} eq 'DUMMY' !!
> > 
> > Last,
> > 
> >     tie $foo{bar} => 'MaiTai';
> > 
> > works fine.  So the problem appears to be autovivifying, through an
> >    alias,
> > in tie.  It would be cool if this worked.
> > 
> > Here is a tie implementation to test with:
> > 
> >     package MaiTai;
> >     sub TIESCALAR { bless {} => MaiTai; }
> >     sub FETCH { 'TIED' }
> 
> I am in the middle of fixing this, but I have run into something
> interesting.
> 
> Obviously sub { tie $_[0]... } ->($h{nonexistent}) should vivify the
> element.
> 
> But should tied $_[0] vivify it?  What about untie?
> 
> These two functions do already autovivify in another way:
> 
> $ ./perl -lIlib -e 'tied %$_; print $_'
> HASH(0x7fa2640052b8)
> $ ./perl -lIlib -e 'untie %$_; print $_'
> HASH(0x7f8d688052b8)
> 
> Should they?
> 
> It makes some sense for untie to put its argument in vivifying context,
> since it modifies it, though it would work just as well without
> autovivifying.
> 
> To me, it doesn’t make sense that tied() should autovivify at all.
> 
> However tied %$_ works, should the same rules apply to tied $_[0] where
> $_[0] represents a nonexistent element?

I have fixed this bug in commit 13733cde.  I chose not to vivify
elements in tied and untie.  Since there is no precedent for this sort
of thing, I picked one behaviour and went with it.  I think preferring
not to vivify when it is not strictly necessary is better.  I did not
touch the existing vivification of references.

-- 

Father Chrysostomos


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=27010

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