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

How can we do abort then return on true condition in a regex as
$n=0;$m=2; sub l{ 'oXoXoXoX' =~ /(?:.X(?{return "Yes n=$n" if ++$n==$m}))*/ } print &l; 1 fails ?

Please guide to the correct one

Replies are listed 'Best First'.
Re: Have to abruptly return in a regex
by LanX (Saint) on Jan 10, 2022 at 16:17 UTC
    this

    > "oXoXoXoX" =~ /(?:.X(?{return "Yes n=$n" if ++$n==$m}))*/

    looks like a very complicated way of trying

    DB<44> sub func { my ($m)=@_; return "Yes n=$m" if 'oXoXoXoX' =~ /(? +:.X){$m}/ } DB<45> p func(0) Yes n=0 DB<46> p func(2) Yes n=2 DB<47> p func(4) Yes n=4 DB<48> p func(5) DB<49>

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Have to abruptly return in a regex
by LanX (Saint) on Jan 10, 2022 at 16:05 UTC