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

(\@a) = \($x,$y) in non-void context

Thread Next
Dave Mitchell
October 7, 2016 09:31
(\@a) = \($x,$y) in non-void context
Message ID:
Since 5.22.0 with the refaliasing feature, you can do, e.g.

    use feature 'refaliasing';
    my @a;
    my ($a, $b) = 1..2;
    \(@a) = \($a, $b);
    print "a=(@a)\n";    # prints (1 2)
    ($a, $b) = 11..12;
    print "a=(@a)\n";    # prints (11 12)

where the elements of @a become aliases of $a, $b.  All good so far.

However, the docs don't seem to cover what happens when such an assign
expression is used in a non-void context.  Also, nothing in the test
suite uses such an expression in a non-void context.

So my specific question is what should these do?

    @b    = (\(@a) = \($a, $b));
    \(@b) = (\(@a) = \($a, $b));
    (\(@a) = \($a, $b)) = \($c, $d);

my gut feeling is that they should be equivalent to

    \(@a) = \($a, $b);  @b    = \(@a); 
    \(@a) = \($a, $b);  \(@b) = \(@a); 
    \(@a) = \($a, $b);  \(@a) = \($c, $d);

In the current implementation the first two cases are equivalent;
however, in the last (lval) case they differ; with this:
    \(@a) = \($a, $b);  \(@a) = \($c, $d);

@a[0,1] are left aliased to $c and $d, as you'd expect; with this:

    (\(@a) = \($a, $b)) = \($c, $d);

@a[0,1] are left aliased to $a and $b.

"I do not resent criticism, even when, for the sake of emphasis,
it parts for the time with reality".
    -- Winston Churchill, House of Commons, 22nd Jan 1941.

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About