in reply to Re: How can I access the number of repititions in a regex?
in thread How can I access the number of repititions in a regex?

The () = puts the regex into list context, then the scalar assignment, as is usual, assigns the countof the items in the list.
An array returns its weight, a list returns its last... a list in scalar context returns its last element, not the count of elements.
  • Comment on array and list return different things in scalar context

Replies are listed 'Best First'.
Re: array and list return different things in scalar context
by kyle (Abbot) on Mar 11, 2008 at 21:00 UTC

    a list in scalar context returns its last element

    There's a somewhat detailed discussion of what's wrong with this idea starting about here. Here's one problem with it:

    my @x = ( 6, 7, 8 ); my $last_element = (4, 5, @x)[-1]; my $as_scalar = (4, 5, @x); print "last element: $last_element\n"; print "as scalar: $as_scalar\n"; __END__ last element: 8 as scalar: 3

    One might say that the scalar context is applied to the last element of the list expression (not the list itself). Note that scalar sub_that_returns_list() will apply the scalar context to everything in the sub's return list, but scalar ( 'literal', 'list', 'expression' ) puts 'literal' and 'list' in void context.

Re: array and list return different things in scalar context
by Errto (Vicar) on Mar 11, 2008 at 20:37 UTC
    This isn't a "list in scalar context", it's a list assignment in scalar context, which has a very well-defined behavior: it returns the number of elements in the list on the right side of the assignment operator.