Pegex::Receiver(3pm)
π Pegex::Receiver π
okay so bestie, welcome to Pegex::Receiver β it's the base class for ALL Pegex receivers, and it lives in the User Contributed Perl Documentation section (that's the 3pm situation, iykyk). it's tiny, it's iconic, and honestly? it's giving minimalism. let's get into it π¦
β¨ SYNOPSIS β¨
here's the whole vibe of how you'd actually use this thing:
package MyReceiver;
use base 'Pegex::Receiver';
# Handle data for a specific rule
sub got_somerulename {
my ($self, $got) = @_;
# ... process ...
return $result;
}
# Handle data for any other rule
sub gotrule {
my ($self, $got) = @_;
return $result;
}
# Pre-process
sub initial { ... }
# Post-process
sub final {
...;
return $final_result;
}
like, that's it. that's the whole assignment. π
πΈ DESCRIPTION πΈ
so in Pegex, a receiver is the class object that a parser passes captured data to when a rule in a grammar matches a part of an input stream. basically: the parser finds stuff, the receiver catches it. a receiver hands you action methods so you can turn that parsed data into whatever the parser was actually supposed to do. main character energy, honestly.
this class right here is the base class of all Pegex receiver classes. π
and here's the gag: it doesn't do much of anything, which is the correct thing to do. no cap. if you use this class as your receiver, it won't do any extra work β zero side quests, zero surprises. and if you do want a receiver base class that'll help organize your matches by default? go look at Pegex::Tree. that one's got your back. π«Ά
π§ How A Receiver Works
a Pegex grammar is made up of three little ingredients: named-rules, regexes, and groups.
- when a regex matches β the parser makes an array of its capture strings.
- when a group matches β the parser makes an array of all the submatch arrays.
and that's how a parse tree forms, bestie. it builds itself. π
then, when a named-rule matches β an action method gets called over in the receiver class. that method gets handed the current parse tree, and whatever it returns becomes the new parse tree as far as the parser is concerned.
the way this makes for such a very elegant and understandable API... obsessed. truly. ππ
π API π
this section documents the methods you can include in a receiver subclass. like, these are the ones you're allowed to write. go off.
got_$rulename($got)β an action method for a specific, named rule. for example:
sub got_rule42 {
my ($self, $got) = @_;
...
return $result;
}
the $got value that gets passed in is the current value of the parse tree. and what you return? that becomes whatever you want the new value to be. your call, your rules. β¨
gotrule($got)β the action method for a named rule that does not have a specific action method of its own. the catch-all bestie.initial()β called at the beginning of a parse operation, before the parsing begins. the little pre-game pep talk. πfinal($got)β called at the end of a parse operation. and listen: whatever this action returns will be the result of the parse. no pressure or anything. π
β Methods
parserβ an attribute containing the parser object that is currently running. this is very useful for introspecting what's happening, and possibly modifying the grammar on the fly. (Experts only! like, genuinely. don't just yolo this. π₯)flatten($array)β a utility method that can turn an array of arrays into a single array. for example:
$self->flatten([1, [2, [3, 4], 5], 6]);
# produces [1, 2, 3, 4, 5, 6]
Hashes are left unchanged. and heads up: the array is modified in place, but it's also the return value. so like, both. it's giving multitasking. π«
π AUTHOR π
Ingy dΓΆt Net <ingy@cpan.org>
π COPYRIGHT AND LICENSE π
Copyright 2010-2020. Ingy dΓΆt Net.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See <http://www.perl.com/perl/misc/Artistic.html>
and that's the whole page, bestie!! π this wasperl v5.36.0, dated2022-11-19β shoutout to thePegex::Receiver(3pm)era, we love a tiny base class that does exactly nothing on purpose. slay, stay parsed, mwah πβ¨π¦
π¦ where this page comes from
- debian bookworm: libpegex-perl 0.75-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libpegex-perl 0.75-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libpegex-perl 0.75-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libpegex-perl 0.75-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libpegex-perl 0.75-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms