Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Transitive aliases

by Anonymous Monk
on Dec 28, 2009 at 01:45 UTC ( #814568=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://814568]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-05-28 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (166 votes). Check out past polls.

    Notices?