getting at the "invoked" tied variable

--XsQoSWH+UP9D9v3l
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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

--=20
rjbs

--XsQoSWH+UP9D9v3l
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQEcBAEBAgAGBQJafeW6AAoJEOYby6cMccU5QmwIAI/AKrfgNahbMcE913dX9xTt
Xfa/amZ87dcdzjS10MAQ6UYZHXTJjopMUwQteeIYKDzk+PeLvEFNYz9IGJ6O25Jw
UuSaSnMKKA3xqsP5w1BGOITsWIS4OOq/X8Qn8dJawpK+jyDO4SUeHe5cfHy2ORfV
geidhGeoy1wo0ok5FxD7C7+NvLzHoHLiGhBoxQHqzh/+odqvU0hPrWWXwjWbxOxf
9mvVf5YPW/snlIQPSU8GqmfomvM/Oa/nUyQKJE4m535FE3cqamheLvM5YzCnF0gS
3C/vS2UKid/MeJktZsH5Nr5Vc1CBTSOgllYWOj0cB0vfoUGLtFZTrnPIPEy5J3M=
=XqtK
-----END PGP SIGNATURE-----

--XsQoSWH+UP9D9v3l--
0
perl
2/9/2018 6:17:30 PM
perl.perl5.porters 47285 articles. 0 followers. Follow

5 Replies
58 Views

Similar Articles

[PageSpeed] 39

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.
0
sprout
2/9/2018 7:44:56 PM
--RnlQjJ0d97Da+TV1
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

* Father Chrysostomos <sprout@cpan.org> [2018-02-09T14:44:56]
> 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.

I have used Variable::Magic before, but I am an amateur.  Are there ordering
concerns that you think I should be aware of (tie then cast, cast then tie)=
 or
anything else?  I may give this a go, if I get stuck in an elevator sometime
soon.

--=20
rjbs

--RnlQjJ0d97Da+TV1
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQEcBAEBAgAGBQJafyQqAAoJEOYby6cMccU5atgIAI9iVQX1PJnN49QTgC3hWE16
ydMuASrxMQagGObLBc1skqKHNn5hg+icdUs9BQDo5YcP6rXv5Ynr/VBIFaiEkUIv
X9tObryYV+fuPGohW0Qdob91vBMtigWJNb2aeIl0l3MdNpztIOdWtEH6RaNOXT38
e0eLUslHVy+H3hWvinLG+LlZv8/pBgJX1WUf/thpON6DM6NSXnJ0/0ZYuwTi+iBS
szdNbgyucjdPJnlWBx8xdLIHZb7uWetJma9eRtipH4Y0bBHhZL6hfxBTrdmK5d3f
9h6aMEnMnO/JQOQgtIJdXHioZkFUkXn/FxxTaQuCsMs3IPalhP63edneYZtg/dw=
=YaZE
-----END PGP SIGNATURE-----

--RnlQjJ0d97Da+TV1--
0
perl
2/10/2018 4:56:11 PM
Ricardo Signes wrote:
> * Father Chrysostomos <sprout@cpan.org> [2018-02-09T14:44:56]
> > 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.
> 
> I have used Variable::Magic before, but I am an amateur.  Are there ordering
> concerns that you think I should be aware of (tie then cast, cast then tie) or
> anything else?  I may give this a go, if I get stuck in an elevator sometime
> soon.

My previous response was rather quick and I had not thought it
through.  It's tricky.

If you want to localise your tied-variable variable properly, then you
will have to forego Perl's tie mechanism and implement your own, which
you will of course call from your own get- and set-magic routines.

If you think that recording the tied variable does not need to be
undone after the FETCH/STORE call (so the scope is similar to $!),
then cast after tying.  The last magic to be attached is trig-
gered first.

Of course, I have not actually tested any of this.
0
sprout
2/11/2018 6:32:19 AM
I wrote:
> My previous response was rather quick and I had not thought it
> through.  It's tricky.
> 
> If you want to localise your tied-variable variable properly, then you
> will have to forego Perl's tie mechanism and implement your own, which
> you will of course call from your own get- and set-magic routines.
> 
> If you think that recording the tied variable does not need to be
> undone after the FETCH/STORE call (so the scope is similar to $!),
> then cast after tying.  The last magic to be attached is trig-
> gered first.

And you would probably want to store a weak reference to the tied var-
iable if you use that approach.
0
sprout
2/11/2018 6:07:45 PM
Ricardo Signes wrote:
>This lets tied objects untie themselves as it becomes practical.

Sounds like a recipe for trouble.  We have too much confusion between
container and value as it is.  Adding more by changing the established
boundary of the tie mechanism is unappealing.  The fact that there
isn't an existing place to put the variable reference, requiring you to
invent something as kludgey as a new dynamically-scoped global variable
(to which your tied_invocant() function is equivalent), ought to be a
clue that this isn't a good conceptual fit.

-zefram
0
zefram
2/16/2018 1:06:03 AM
Reply: