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...
}
|