in reply to using TIEHASH as wrapper/proxy, while avoiding recursion and warnings

It's unclear to me what your proxy class is actually supposed to be doing?

Anyway, remember when I wrote Tie::Subset::Hash inspired by your post? Maybe the source can help as a starting point.

  • Comment on Re: using TIEHASH as wrapper/proxy, while avoiding recursion and warnings

Replies are listed 'Best First'.
Re^2: using TIEHASH as wrapper/proxy, while avoiding recursion and warnings
by LanX (Saint) on Apr 22, 2024 at 10:52 UTC
    The wrapper in Tie::Subset::Hash is a new variable.

    I wanted to be able to tie the original variable while changing the original data.

    This creates a lot of headaches to even access that data, to avoid recursion and related warnings.

    > It's unclear to me what your proxy class is actually supposed to be doing?

    Consider debugging, setting watchpoints, tracing, meta programming,...

    It's not always possible to replace the monitored variable with a proxy, you just want to attach the magic directly.

    There is code in the OP.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      I wanted to be able to tie the original variable while changing the original data.

      I see. I remember that when I tried something similar to that with tied filehandles, I got Perl to segfault, so I'm not sure if tie is meant to be used that way.

      It's not always possible to replace the monitored variable with a proxy, you just want to attach the magic directly.

      That's probably true, but I imagine it wouldn't be too difficult to refactor the code in question from using a hash to a hashref?

      Otherwise, if this is just for debugging, you could use refaliasing to replace the target hash with the tied hash? (though to unite you'd probably have to keep a reference to the original hash around)

      use warnings; use strict; use feature 'refaliasing'; use Data::Dump; use Tie::Subset::Hash; my %hash; @hash{"a".."e"} = 41..45; dd \%hash; tie my %hash2, 'Tie::Subset::Hash', \%hash, ['b']; \%hash = \%hash2; dd \%hash; __END__ Aliasing via reference is experimental at foo.pl line 13. { a => 41, b => 42, c => 43, d => 44, e => 45 } { # tied Tie::Subset::Hash b => 42, }