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 @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]
Even more ephemeral. But the "$count = () = m//" construct can be handy occasionally.my $count = () = "abacadaeafag" =~ /a/g; my @empty = () = "abacadaeafag" =~ /a/g; print "[", $count, "]\n"; # prints: [6] print "[", @empty, "]\n"; # prints: []
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |