http://qs321.pair.com?node_id=814568


in reply to Transitive aliases

This may be missing the mark, because I don't understand your goal; but for the way switch_var is supposed to work, inside of that sub you could tie $w. Then setting it elsewhere in the program automatically sets $r. Based on my rememberance of subroutine prototypes, maybe something like:

sub switch_var (\$$\$) { my ($reference, $initial, $binding) = @_; # bind $binding to scalar reference $reference tie $$binding, 'SomeClass', $reference; $$binding = $initial; }
where SomeClass is a simple scalar tied package that implements at least TIE to store $reference and STORE to set @$reference = ($value) x $something.

Replies are listed 'Best First'.
Re^2: Transitive aliases
by JadeNB (Chaplain) on Dec 28, 2009 at 03:18 UTC

    This is a good idea, although I think I may have made the problem statement over-specific—I'd really like to be able to handle any circular reference, not just a reference to an array of several copies of a single variable. Thus, maybe the appropriate thing to do would be

    sub switch_var { tie $_[2], to => $_[1]; } package to; sub TIESCALAR { bless \$_[1] => $_[0]; } sub STORE { ${$_[0]} = $_[1] } sub FETCH { ${$_[0]} }
    (There's some package out there already on CPAN that uses the to package for similar effect, but I can't find it. UPDATE: It's Tie::Util. Also, the functionality that I've described is quite similar—indeed, maybe identical—to that provided by Tie::Coupler.)

    I wonder if there's a non-tie-based solution? I might have to do switch_var several times, and going through many ties could get quite slow.