next up previous contents index PLPL moodlepserratamodulosperlmonksperldocapuntes LHPgoogleetsiiullpcgull
Sig: Manejo y recuperación de Sup: Análisis Sintáctico con Regexp::Grammars Ant: Mensajes de log del Err: Si hallas una errata ...

Depuración de Regexps

It is possible to use Regexp::Grammars without creating any subrule definitions, simply to debug a recalcitrant regex. For example, if the following regex wasn't working as expected:

        my $balanced_brackets = qr{
            \(             # left delim
            (?:
                \\         # escape or
            |   (?R)       # recurse or
            |   .          # whatever
            )*
            \)             # right delim
        }xms;

you could instrument it with aliased subpatterns and then debug it step-by-step, using Regexp::Grammars:

        use Regexp::Grammars;

        my $balanced_brackets = qr{
            <debug:step>

            <.left_delim=  (  \(  )>
            (?:
                <.escape=  (  \\  )>
            |   <.recurse= ( (?R) )>
            |   <.whatever=(  .   )>
            )*
            <.right_delim= (  \)  )>
        }xms;

        while (<>) {
            say 'matched' if /$balanced_brackets/;
        }

Note the use of amnesiac aliased subpatterns to avoid needlessly building a result-hash. Alternatively, you could use listifying aliases to preserve the matching structure as an additional debugging aid:

        use Regexp::Grammars;

        my $balanced_brackets = qr{
            <debug:step>

            <[left_delim=  (  \(  )]>
            (?:
                <[escape=  (  \\  )]>
            |   <[recurse= ( (?R) )]>
            |   <[whatever=(  .   )]>
            )*
            <[right_delim= (  \)  )]>
        }xms;

        if ( '(a(bc)d)' =~ /$balanced_brackets/) {
            use Data::Dumper 'Dumper';
            warn Dumper \%/;
        }


next up previous contents index PLPL moodlepserratamodulosperlmonksperldocapuntes LHPgoogleetsiiullpcgull
Sig: Manejo y recuperación de Sup: Análisis Sintáctico con Regexp::Grammars Ant: Mensajes de log del Err: Si hallas una errata ...
Casiano Rodríguez León
2012-05-22