Forsaken has asked for the wisdom of the Perl Monks concerning the following question:

Most Esteemed Monks,

what I am attempting to do is a one-line extraction from a regular expression. Now, normally when extracting the results are stored in $1, $2 and so on, and according to the docs when the regex is used in array-context like this:
my @matches = ($text =~ /(stufftoextract)\w(morestufftoextract)/);
the results will be stored in @matches all nice and tidy. however, what i'm a bit curious about is how to approach the extraction of a single element without having the result stored in an array. When using a similar extraction in scalar context like this:
my $match = $text =~ /(stufftoextract)/;
$match will be 1 to indicate a succesful match was made. Of course there is a relatively simple method to actually get to the result as follows:
my $match = ($text =~ /(stufftoextract)/)[0];
What I'm wondering about now is whether there's other ways to get to the same result in a single line of code, so first matching and simply assigning the value of $1 to $match doesn't count ;-)

Remember rule one...

Replies are listed 'Best First'.
Re: One-liner regex extraction
by ikegami (Patriarch) on May 18, 2005 at 19:00 UTC

    Put $match in list context by adding parens:

    my ($match) = $text =~ /(stufftoextract)/;
      And the Forsaken one bashed his head against a concrete wall, and there was much rejoicing in all of the kingdom, for the evil one had been slain.

      Duh! Why didn't I think of that!?

      Remember rule one...

        /me pities the poor Wall.


        holli, /regexed monk/