in reply to Re: /g matches not really global in scalar context!
in thread /g matches not really global in scalar context!

Don’t confuse yourself. foreach puts its expression into list context, so a /g match actually returns a list of all matches at once; of course the foreach then iterates over them one by one. The loop construct you want is while, which evaluates its condition in scalar context, so a /g match as the condition will return matches one by one.

Makeshifts last the longest.

  • Comment on Re^2: /g matches not really global in scalar context!

Replies are listed 'Best First'.
Re^3: /g matches not really global in scalar context!
by jonix (Friar) on Nov 14, 2005 at 14:37 UTC
    Thanks for making this clear. Is there a way to get all those /g matches at once without a loop?
    OK just found it:
    use warnings; use strict; my $x = "aiutino aiutino argh! aiutino"; my @matches = $x =~ / \b aiutino \b /gix; print "$_\n" for @matches;
    so it is depending on scalar vs. list context what you will get.
    Thanks again,
    jonix