develooper Front page | perl.perl6.users | Postings from August 2019

perl6-regex: retaining $/.pos after an unsuccesful match without atemporary variable?

Thread Next
From:
Raymond Dresens
Date:
August 18, 2019 14:48
Subject:
perl6-regex: retaining $/.pos after an unsuccesful match without atemporary variable?
Message ID:
CALBLdeaigfweTp6TkX-ass7Z_y865qYP8Ubaw3w3gCxDnmpmig@mail.gmail.com
Hello,

In the past few days I've been converting some "incremental parsing"-regex
code from perl 5 to perl 6 (I haven't not touched grammars, yet...)

In Perl 5 I often used the /g and /c modifiers so that the following
snippet of code doesn't die:

    # perl5

    my $test = "      foo bar";

    die unless $test =~ /^\s+/g;
    die unless $test =~ /\Gfoo\s+/g;
    die if     $test =~ /\Gwillnotmatch/gc; # ...what is the equivalent of
the 'c' modifier in Perl 6?
    die unless $test =~ /\Gbar/g;

    say pos $test; # yields "13"

I managed to translate such code in Perl 6, by using the "m:p/.../" regex,

When I anticipate an unsuccessful match then I must temporarily store $/.pos
and provide it to the next regex (e.g. "m:p($P)//"), so it seems,

That works.... but it riddles my current solutions with "$P = $/.pos;"
assignments.

Is there a way to retain this match like in Perl 5?

Is there a better way in general?

... perhaps it's time to look into grammars? ;)

Thanks!

Regards,

Raymond Dresens.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About