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

Re: getting at the "invoked" tied variable

Thread Previous | Thread Next
From:
Father Chrysostomos
Date:
February 9, 2018 19:45
Subject:
Re: getting at the "invoked" tied variable
Message ID:
20180209194456.16307.qmail@lists-nntp.develooper.com
Ricardo Signes wrote:
> This is an idea that I had today, and it seemed useful, but I'm not sure how
> technically plausible it is.
> 
> Imagine that there exists a routine called tied_invocant.  Inside a
> tied-variable method like FETCH, it returns a reference to the variable that
> caused the FETCH invocation.  That makes this possible:
> 
>   package Thunk::Random {
>     sub TIESCALAR { bless {} }         
> 
>     sub FETCH ($self) {
>       $self->{value} //=3D $rand;
> 
>       my $tied =3D tied_invocant;
>       untie $$tied;
>       $$tied =3D rand;
> 
>       return $$tied;
>     }
>   }
> 
> This lets tied objects untie themselves as it becomes practical.  There may be
> plenty of reasons that this isn't worth making possible, like availability of
> the invocant or garbage collection of $self after untie.

I do not see how that would be a problem.  BTW, did you know that you
can assign to $_[0] to change what object it is tied to?  However,
that does not help here.  (Similarly, you can do $foo->CORE::undef to
undefine $foo.)

> I dunno, it just
> seemed like an idea worth getting out of my head and to the list.

I have wanted this feature myself before. I do not remember what I
intended to use it for though.

I think the most feasible way to may this work is to record the tied variable in a global, localized variable just before calling FETCH.

This is actually possible to do in a CPAN module if the module
globally overrides tie() and attaches its own magic in addition to
the tie magic.

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