in reply to Re: Forcing array context
in thread Forcing array context

for the benefit of those (or I) who are reading with interest. jryan can you untangle this a bit?.
()= I understand as filling an anonymous list with whatever is to the right of =. Forcing list context.
But what is being evaluated by the regex? just the first element of the @_ array ?
Deductivly the enclosing ( .... ) parens do magic on @_ but WTF? has anyone ever written a 'golf disassembler' ?

submersible_toaster
-they all say 'my computer is frozen!'. just once couldn't one catch fire?

Replies are listed 'Best First'.
Re: Re: Re: Forcing array context
by broquaint (Abbot) on Nov 22, 2002 at 11:14 UTC
    But what is being evaluated by the regex? just the first element of the @_ array ?
    The regex match is doing just what is in the code - matching the first element of @_ with the regex. The effect of the list context is that whatever is matched is returned e.g
    print map("[$_]", "foo bar baz quux" =~ /b\w+ /g), $/; __output__ [bar ][baz ]
    This is the result of the g modifier in list context without capturing parens. With capturing parens however it returns a list of what was captured e.g
    print map("[$_]", "foo bar baz quux" =~ /(b\w+) /g), $/; __output__ [bar][baz]
    For more info regarding regex matching and context see the Regexp Quote-Like Operators section in perlop.
    HTH

    _________
    broquaint

Re: Re: Re: Forcing array context
by jryan (Vicar) on Nov 22, 2002 at 19:38 UTC

    I think that the confusion is my fault. You are correct that () = is forcing the matches of the regular expression to fill an anyonomous list (which should get optimized out), and are also correct that the match is being evaluated against the first element of @_ (in this case, the first argument to a subroutine). However, there is no magic being done on @_. The enclosing parens could have been completely left out. Here is my code fleshed out a little more:

    sub expand_number_list { my @matches; () = ($_[0] =~ / (match something) (??{munge the match and push it to @matches}) /xg); return @matches; }

    Generally, the point is that just adding () = will force your expression into list context. Please see Forcing list context for more details.

      ++jryan has answered my questions. ty