in reply to Scalar assignment question

my $scalar forces the return value of grep to a scalar context, in which case, it returns the number of matches.

my ($scalar) forces the return value of grep to a list context, in which case it returns the first match.

Consider:

use strict; use warnings; # MATCHES { my $scalar = grep /\d+/, 1 .. 5; print "$scalar\n"; } { my ($scalar) = grep /\d+/, 1 .. 5; print "$scalar\n"; } # NO MATCHES { my $scalar = grep /\d+/, 'a' .. 'z'; print "$scalar\n"; } { my ($scalar) = grep /\d+/, 'a' .. 'z'; print( (defined $scalar) ? "$scalar\n" : "undefined\n" ); } __END__ 5 1 0 undefined

See also: List value constructors

Update: added 'no matches' to example