What shall we call these? "Ephemeral list elements"? As you know, merlyn, they actually last a little longer then that piece of doc implies. They are not just immediately thrown away. They last long enough for us to grab the value that undef held a place for. Some examples will help here:
my @w = () = 1; print "[", @w, "]\n"; # prints: [] my @x = (undef) = 2; print "[", @x, "]\n"; # prints: [2] my @y = (undef) = (3,4); print "[", @y, "]\n"; # prints: [3] my @z = (undef, undef) = (5,6); print "[", @z, "]\n"; # prints: [56] my $temp; my @z = (undef, $temp, undef) = (7,8,9); print "[", @z, "]\n"; # prints: [789]
All of which is related to but a bit different from the following which (like the first example above) does not return values-in-a-list but still manages to force a list context for what will be returned by the regex (or any other expression that can return scalar or list). That elusive ghost list can then be construed into a scalar count of the list elements:
my $count = () = "abacadaeafag" =~ /a/g; my @empty = () = "abacadaeafag" =~ /a/g; print "[", $count, "]\n"; # prints: [6] print "[", @empty, "]\n"; # prints: []
Even more ephemeral. But the "$count = () = m//" construct can be handy occasionally.

Update: a friend asked for an example of 'handy'. Herewith offered:

$_ = "abacadaeafag"; # or whatever if ( scalar( () = /a/g ) > 7 ) { # more than 7 'hits'? # do something... }

In reply to Re: Re: undef as an lvalue by dvergin
in thread undef as an lvalue by MrNobo1024

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.