Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The following amusing piece of code gives a reference to a length-2 array whose 0th and 1st entries may be changed, but are always equal to one another:

sub double { sub { \@_ }->($_[0], $_[0]) } my $r = double my $v; $v = 1; @$r; # => ( 1, 1 ); $v = 2; @$r; # => ( 2, 2 ); $r->[0] = 3; @$r; # => ( 3, 3 );

I would like to have a 3-argument subroutine switch_var such that the following code works:

switch_var $r, $v, my $w; $w = 4; @$r; # => ( 4, 4 );

Of course, a cheating way to do it is as follows:

sub switch_var { $_[0] = double $_[2] }
This satisfies the letter of my requirement, but not the spirit, because it only works for the particular $r I've given, not for an arbitrary circular reference.

Since the responses to Local for lexicals taught me the lesson of How to force one variable to be an alias to another?, I bethought myself of Data::Alias, but it doesn't work:

sub switch_var { alias $_[1] = $_[2]; } switch_var $r, $v, my $w; $w = 4; @$r; # => ( 3, 3 );
Even if I assume that I know something about the structure of the self-reference $r, I can only get at half of it:
sub switch_var { alias $_[0][0] = $_[2]; } switch_var $r, $v, my $w; $w = 4; @$r; # => ( 4, 3 )
I think that this is the behaviour mentioned in KNOWN ISSUES:
When aliasing existing lexical variables, the effect is limited in scope to the current subroutine and any closures create after the aliasing is done, even if the variable itself has wider scope. While partial fixes are possible, it cannot be fixed in any reliable or consistent way, and therefore I'm keeping the current behaviour.
Thus, I guess that what I'm specifically wondering here is: Does the behaviour that I want admit one of the partial fixes mentioned? If so, what is it?

Oh, and, since Local for lexicals also taught me that I should explain the ‘why’ as well as the ‘what’: What I'm trying to do is make fake subroutines for a combinator library I'm writing (sort of as a replacement for Sub::Compose, which is lovely but inherits the limitations of Data::Dumper::Streamer), so that I can write something like:

my $x; my $fake_sub = fake_subroutine $x => sub { \@_ }->($x, $x); my $r = call $fake_sub, my $y; $y = 1; @$r; # => ( 1, 1 );
The obvious response is “Why not use real subroutines?”, but it doesn't work for me: I'm using some trickery elsewhere so that I can tell the combinator SKK, say, to expect 1 argument $x, and it will figure out once and for all that it will just return $x (whatever it happens to be). The trickery I'm using (which is, literally, just calling my fake subroutine with a fresh variable $x and noting down the result as a ‘template’ for future calls) doesn't play well with real subroutines.


In reply to Transitive aliases by JadeNB

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-24 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found