https://metacpan.org/pod/Syntax::Keyword::Assert
The assert { ... }
syntax allows you to create assertions based on the evaluation of a block. One of the key advantages of using a keyword plugin like this is that it doesn’t degrade performance in production environments where assertions might be unnecessary. This is similar to how assertions work in compiled languages.
Here’s a simple example:
use Syntax::Keyword::Assert;
sub hello($name) {
assert { defined $name };
say "Hello, $name!";
}
hello("Alice"); # => Hello, Alice!
hello(); # => Dies when STRICT mode is enabled
Keyword plugins hook into Perl’s parser to extend its syntax. Paul Evans, a Perl developer, has been making extensive use of this feature. Instead of directly modifying Perl’s core, he prototypes new features using keyword plugins, gathers feedback from the community, and then works towards incorporating them into Perl itself. Recent syntax extensions like try/catch, defer, and class are results of this approach. (a remarkable achievement!).
Examples of Modules Using Keyword Plugins:
How Do Keyword Plugins Work?
Here’s a general idea of how keyword plugins function:
- The parser detects the specified keyword (in this case,
assert
). - It inserts the desired OP tree (similar to an abstract syntax tree) around the keyword. That's it!
For instance, in the case of Syntax::Keyword::Assert
, the code assert { defined $name }
gets transformed into an OP tree equivalent to the following:
# When STRICT mode is enabled
croak 'Assertion failed' unless do { defined $name };
# When STRICT mode is disabled, do nothing.
For those interested in the source code, here’s how it works:
Pretty interesting, right?
Development Notes
As a side note, I wasn’t initially familiar with constructing OP trees, so I’ll document my process here for future reference.
Here’s the general workflow I followed:
- First, write the content you want to replace with a keyword in pure Perl. For this example, it’s
croak ... unless BLOCK
. - Use tools like
B::Terse
andB::Debug
to understand the resulting OP tree. - Verify that the OP tree you’ve written matches the one from the original code.
- Additionally, check the output with
B::Deparse
to ensure it matches your expectations. - Iterate until you get the desired results.
I also found the following resources helpful:
- The source code of other modules that use keyword plugins.
- Since this syntax involves
KEYWORD BLOCK
, it’s essentially the same asSyntax::Keyword::Defer
, apart from the OP tree construction.
- Since this syntax involves
- Reading
perlapi
and examining core Perl files likeop.c
andopnames.h
made it easier to understand OP tree construction.- If you’re unclear on terms like SV or OP, I recommend reading this article by tokuhirom: https://xsubtut.github.io/ (jp).
- I also frequently used
Yet Another CPAN Grep
to search for relevant code.
That’s all for now!
Happy Hacking!
Top comments (4)
Please join us and cross post at facebook.com/groups/perlprogrammers and facebook.com/groups/perlcommunity . We've had too many AI generated posts on basic topics lately and could use one on an advanced topic from a real-live human.
Thanks to invite them!
This is what the Perl crowd wants to read! ❤️
Thanks for the kind words! 😁