develooper Front page | perl.perl5.porters | Postings from April 2007

Re: Proposed changes and to regular expression interfaces in core

Thread Previous | Thread Next
From:
Ævar Arnfjörð Bjarmason
Date:
April 3, 2007 18:16
Subject:
Re: Proposed changes and to regular expression interfaces in core
Message ID:
51dd1af80704031816q385fcf12g3f5994a66bf1529f@mail.gmail.com
On 4/3/07, Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:
> On 4/2/07, Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:

I've also found that length() is magical with numbered captures:

use re::engine::Plugin (
    exec => sub {
        my ($re, $sv) = @_;

        $re->captures( [ "ook" ] );

         1: # successful match
     }
 );

"str" =~ /pattern/;

warn $1; # "ook"
warn length($1); # 0

This is because Perl_magic_len gets called for $1, that function
checks rx->offs which doesn't do what's expected when $1 isn't really
stored there.

I think the best way to solve this is to have tie-like callbacks for
named captures in the regexp_engine struct (names modelled after
Tie::Scalar):

FETCH(pTHX_ const REGEXP * const rx, I32 paren, SV* usesv);
STORE(pTHX_ const REGEXP * const rx, I32 paren, SV* usesv, SV* sv);
LENGTH(pTHX_ const REGEXP * const rx, const char * const mg_ptr);

This would allow for fast length() by having the perl engine do what
it does currently in mg.c in some engine-specific function, allow for
$1 = "foo" via STORE which the perl engine would implement by calling
Perl_croak().

I'm not really sure what datatypes to use, mg_ptr which is used in
Perl_magic_len can be $1 .. $9, $&, $`, $'  and also $+ and $^N.

Similarly the engine would implement a Tie::Hash-like interface for %+ and %-.

Thread Previous | 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