No, the behavior of assignment and the x operator are defined for array slices.

Both () x EXPR and () = EXPR are defined, but () x= EXPR is another operator and doesn't work like you'd expect; in fact, () x= isn't documented and x is the only operator that can be used with = that has special semantics for its LHS operand. All other force scalar context so it's a no-brainer how they'll work for list assignment. perlop says that it works like in C, and you can then assume the following paragraph is about scalar assignment and it always talks about a singular lvalue. Only in the last paragraph list assignment is covered.

In (context()) = EXPR the subroutine &context is called in list context; in (context()) x= EXPR it's called in scalar context. However, the lvalue of all elements in the list in (LIST) x= EXPR are taken (hence the autovivification), just as for

sub foo {} foo(@foo{qw/a b/}); print keys %foo; # ab
The lvalues are then put in scalar context making them not a list anymore but a sequence of scalars with the scalar-comma between them (see perlop). Why this happens I have no idea. As a consequence of the "list" of elements being "comma-separated in scalar context" all values but the last are put in void context and the last in scalar context. Example:
use warnings; sub context { defined $_[0] ? $_[0] ? 'list' : 'scalar' : 'void' } sub foo :lvalue { print context(wantarray); my $dummy = '' } sub bar :lvalue { print context(wantarray); my $dummy = '' } my $baz; ($baz, foo(), bar()) x= 3; __END__ Useless use of private variable in void context void scalar

Is \ documented for array slices?

From perlref

Taking a reference to an enumerated list is not the same as using square brackets--instead it's the same as creating a list of references!

The explicit parenthesis isn't needed. (Just consider \qw/ foo bar /.) @foo[@bar] is a listop called aslice. I don't know if it's documented that aslice may be used instead of a list, so you may be right. It's at least more documented than the other tricks. ;-)

ihb

See perltoc if you don't know which perldoc to read!


In reply to Re^7: Adding Unique Elements to Array by ihb
in thread Adding Unique Elements to Array by thekestrel

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.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.