The return value of the "=~" operator (if called in list context) is the array you desire.

use Data::Dumper; if (my @r = "foo bar baz" =~ /(foo) (bar) (baz)/) { print Dumper \@r } __END__ $VAR1 = [ 'foo', 'bar', 'baz' ];

You say:

Can we get hold of that array explicitly somehow when we are not in list context?

... so clearly you already know the above. Why not just use list context? There is rarely any reason to explicitly avoid it when regexp matching. Please explain what you're trying to actually do, and why performing matches in list context is insufficient.

Depending on what you're trying to do, you could consider using named captures, which get stored into the hash %-. If you name the captures appropriately, you could assemble them into an array...

sub get_caps () { my @caps; my $i = 1; while (exists $-{'cap'.$i}) { push @caps, $-{'cap'.$i++}->[0]; } @caps; } use Data::Dumper; if (scalar("foo bar baz" =~ /(?<cap1>foo) (?<cap2>bar) (?<cap3>baz)/)) { my @r = get_caps; print Dumper \@r; } __END__ $VAR1 = [ 'foo', 'bar', 'baz' ];

Though obviously that involves modifying the regular expression itself to add the named captures. And it needs a non-archaic version of Perl (at least 5.10).


In reply to Re: Is there a default array for reg exp memory variables? by tobyink
in thread Is there a default array for reg exp memory variables? by korpenkraxar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.