in reply to Re: Cool example of list assignment usage
in thread Cool example of list assignment usage

Okay, that is pretty confusing. Wouldn't you expect the outer parentheses to return $first in list context, rather than the number of matches from the grep?

No, I don't find it confusing at all, but maybe that's because while not claiming in any way to be a Perl guru or anything, fundamental bits and pieces of Perl semantics are deeply etched in my cortex. I don't expect the outer parens to return $first in list context because it's the same mechanism by virtue of which the (sometimes) so-called "goatse operator" (do not follow link if you don't already know and are easily impressed) =()= works. No NASA technology, just Perl: and then I find Perl semantics to be intuitive in most situations, with the exception of some corner cases, with the current one possibly falling in the latter category. But for some reasons it's still intuitive to me and, I believe, to most Perl programmers with a bare minimum of confidence with the language.

Replies are listed 'Best First'.
Re^3: Cool example of list assignment usage
by perrin (Chancellor) on Jul 23, 2007 at 15:54 UTC
    I can't guess what =()= is supposed to do. Maybe this is similar to this regex construct, which I use even though it looks very non-intuitive to me:
    my ($match) = ($string =~ m/regex/);
      I can't guess what =()= is supposed to do.

      How strange, it is a very common Perl idiom:

      my $count =()= /$regex/g
      Maybe this is similar to this regex construct, which I use even though it looks very non-intuitive to me:
      my ($match) = ($string =~ m/regex/);

      Why so? Incidentally I think you want a /g in the match, and the second pair of parens is clearly unnecessary.

      Update: did a Super Search and added link to the explanation of the idiom

        I don't think =()= is very common. I've seen a lot of perl code and never seen this idiom. Is it in one of the books or man pages?

        You're correct about the missing /g. The extra parentheses are there to make it easier for me to read, although perl doesn't need them.