### comment on

 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.

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

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2020-06-01 23:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Do you really want to know if there is extraterrestrial life?

Results (12 votes). Check out past polls.

Notices?