DEV Community

kobaken
kobaken

Posted on

Released Syntax::Keyword::Assert

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
Enter fullscreen mode Exit fullscreen mode

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:

  1. The parser detects the specified keyword (in this case, assert).
  2. 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.
Enter fullscreen mode Exit fullscreen mode

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:

  1. First, write the content you want to replace with a keyword in pure Perl. For this example, it’s croak ... unless BLOCK.
  2. Use tools like B::Terse and B::Debug to understand the resulting OP tree.
  3. Verify that the OP tree you’ve written matches the one from the original code.
  4. Additionally, check the output with B::Deparse to ensure it matches your expectations.
  5. 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 as Syntax::Keyword::Defer, apart from the OP tree construction.
  • Reading perlapi and examining core Perl files like op.c and opnames.h made it easier to understand OP tree construction.
  • I also frequently used Yet Another CPAN Grep to search for relevant code.

That’s all for now!

Happy Hacking!

Top comments (4)

Collapse
 
matthewpersico profile image
Matthew O. Persico

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.

Collapse
 
kfly8 profile image
kobaken

Thanks to invite them!

Collapse
 
thibaultduponchelle profile image
Tib • Edited

This is what the Perl crowd wants to read! ❤️

Collapse
 
kfly8 profile image
kobaken

Thanks for the kind words! 😁