develooper Front page | perl.perl5.porters | Postings from October 2014

[perl #20510] lvalue references would be nice

From:
Father Chrysostomos via RT
Date:
October 11, 2014 21:36
Subject:
[perl #20510] lvalue references would be nice
Message ID:
rt-4.0.18-32521-1413063411-537.20510-15-0@perl.org
This feature is now implemented as of commit 05f7c1e8a7.

On Thu Jan 23 01:58:57 2003, whatever@davidnicol.com wrote:
> I would like to see the
> specific handling of this situation altered as follows:
> 
> assignment to a reference produces an alias. 
> 
> References to both lexical and package variables can be assigned to.
> 
> Assigning a reference to a reference produces an alias of
> what is referred to by the rvalue reference.

Those work.

> assigning a non-reference to a reference produces an alias
> to the entity which would be referred to if the nonreference
> were dereferenced as a symbolic reference name.

That I did *not* implement, because the left- and right-hand sides will not be equal after the assignment.  I know we have precedent for that with typeglob assignment, but I think that is bad precedent and a source of bugs.

> 
> Autovivification occurs to support arbitrary depths, so that
> not only
> 	my $bark = 'woof';
> 	my $name = 'bark';
> 	print "$$name\n";
> but also
> 	my $bark = 'woof';
> 	my $name = 'bark'; # or \$bark
> 	my $ref = 'name'; # or \$name
> 	my $ref2 = \$ref;
> 	my \\\$alias = $ref2;

The syntax I used is \my $x, not my \$x, because there was no purpose in introducing new syntax for it.  (The only new syntax I introduced was for ‘foreach \$x’.)

I see little value in allowing assignment to \\\$alias.  It could be used for obfuscation, which could be considered a plus in some circles; but it complicates the internals and provides nothing that couldn’t be accomplished some other way (\my $alias = $$$ref2).

> 	print "$alias\n";
> will print "woof\n".

> 
> After discussion on p5p, it is expected that all types from which
> it is possible to get a reference will be aliasable using lvalue
> references, and that the "special case" of \(@foo), as an lvalue,
> will allow aliases of arrays to be created easily:
> 
> 	my \($first, $second, $third) = \(@_);

Of course, there would be no special-casing here, since the right-hand side would evaluate as it always does.  (But it is \my, not my\.)  Further, you can assign \(@a) = \(@b) to alias the elements, but not the entire array.

-- 

Father Chrysostomos


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=20510



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About