Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How to use Regexp::Common patterns in Regexp::Grammars?

by clueless newbie (Curate)
on Jun 09, 2022 at 14:39 UTC ( [id://11144579] : perlquestion . print w/replies, xml ) Need Help??

clueless newbie has asked for the wisdom of the Perl Monks concerning the following question:

Ave!

Is there a simpler way to use Regexp::Common patterns in a Regexp::Grammar?

use 5.026; use Data::Dumper; use Regexp::Common qw[ delimited ]; use Regexp::Grammars; =Does NOT work! my $grammar = qr{ <token: Quoted_String> ($RE{delimited}{ -delim => q{'} }{ -esc +=>q{'}}) }; =cut my $pattern=''.$RE{delimited}{ -delim => q{'} }{ -esc=>q{'}}; my $grammar = qr{ <nocontext:> <TEST> <rule: TEST> <Quoted_String> <token: Quoted_String> $pattern }; if (q{'some string with containing a '''} =~ $grammar) { warn Data::Dumper->new([\%/],[qw(*/)])->Deepcopy(1)->Indent(1)->Ma +xdepth(3)->Sortkeys(1)->Dump(),q{ }; };
thanks,
clueless

Replies are listed 'Best First'.
Re: How to use Regexp::Common patterns in Regexp::Grammars?
by clueless newbie (Curate) on Jun 09, 2022 at 16:44 UTC

    Try Paraphilic infantilism. LOL - the baby carriage!

    use 5.026; use Data::Dumper; use Readonly; use Regexp::Common qw[ delimited ]; use Regexp::Grammars; my $grammar = qr{ <nocontext:> <TEST> <rule: TEST> <Quoted_String> <token: Quoted_String> @{[ qq[(?: $RE{delimited}{-delim=>q{'}} +{-esc=>q{'}}) ] ]} }; if (q{'some string with containing a '''} =~ $grammar) { warn Data::Dumper->new([\%/],[qw(*/)])->Deepcopy(1)->Indent(1)->Ma +xdepth(3)->Sortkeys(1)->Dump(),q{ }; }; __DATA__
Re: How to use Regexp::Common patterns in Regexp::Grammars? (Properly)
by Anonymous Monk on Jun 09, 2022 at 18:23 UTC

      I've no idea what I'm doing wrong but unfortunately for me, both a bare $RE... and #$RE... fails for me.

      use 5.026; use Carp; use Data::Dumper; use Readonly; use Regexp::Common qw[ delimited ]; use Regexp::Grammars; use Try::Tiny; BEGIN { say "Regexp::Common: $Regexp::Common::VERSION"; say "Regexp::Grammars: $Regexp::Grammars::VERSION"; } try { my $grammar = qr{ <nocontext:> <TEST> <rule: TEST> <Quoted_String> <token: Quoted_String> $RE{delimited}{-delim=>q{'}}{-esc=> +q{'}} }; if (q{'some string with containing a '''} =~ $grammar) { warn Data::Dumper->new([\%/],[qw(*/)])->Deepcopy(1)->Indent(1) +->Maxdepth(3)->Sortkeys(1)->Dump(),q{ }; }; } catch { Carp::cluck $_ }; # line 24 try { my $grammar = qr{ <nocontext:> <TEST> <rule: TEST> <Quoted_String> <token: Quoted_String> #$RE{delimited}{-delim=>q{'}}{-esc= +>q{'}} }; if (q{'some string with containing a '''} =~ $grammar) { warn Data::Dumper->new([\%/],[qw(*/)])->Deepcopy(1)->Indent(1) +->Maxdepth(3)->Sortkeys(1)->Dump(),q{ }; }; } catch { Carp::cluck $_ }; # line 36

      yields

      >perl re.t Regexp::Common: 2017060201 Regexp::Grammars: 1.057 Not a SCALAR reference at C:/berrybrew/5.32.1_64/perl/site/lib/Regexp/ +Grammars.pm line 106. at ..\..\test\re.t line 24. main::catch {...} ("Not a SCALAR reference at C:/berrybrew/5.3 +2.1_64/perl/site/li"...) called at C:/berrybrew/5.32.1_64/perl/lib/Tr +y/Tiny.pm line 123 Try::Tiny::try(CODE(0xf9c1a0), Try::Tiny::Catch=REF(0x2819630) +) called at ..\..\test\re.t line 24 Not a SCALAR reference at C:/berrybrew/5.32.1_64/perl/site/lib/Regexp/ +Grammars.pm line 106. at ..\..\test\re.t line 36. main::catch {...} ("Not a SCALAR reference at C:/berrybrew/5.3 +2.1_64/perl/site/li"...) called at C:/berrybrew/5.32.1_64/perl/lib/Tr +y/Tiny.pm line 123 Try::Tiny::try(CODE(0xf9c1a0), Try::Tiny::Catch=REF(0xe581f8)) + called at ..\..\test\re.t line 36
        Ok. This is a comment describing the source of regex pattern # $RE{delimited}{ -delim => q{'} }{ -esc=>q{'}}

        the idea is to replace ... with the string generated by printing $RE{delimited}{ -delim => q{'} }{ -esc=>q{'}}

        its ok to inline regexp::common patterns . no $pat or @{[ ]} required