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

Re: getting at the "invoked" tied variable

Thread Previous | Thread Next
Father Chrysostomos
February 9, 2018 19:45
Re: getting at the "invoked" tied variable
Message ID:
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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About