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

getting at the "invoked" tied variable

Thread Next
From:
Ricardo Signes
Date:
February 9, 2018 18:18
Subject:
getting at the "invoked" tied variable
Message ID:
20180209181730.GA19612@debian
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} //= $rand;

      my $tied = tied_invocant;
      untie $$tied;
      $$tied = 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 dunno, it just
seemed like an idea worth getting out of my head and to the list.

-- 
rjbs

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