Syntax...:Assert(3pm)

πŸ“¦
version 0.19 Β· 3 versions
, in ubuntu stonking

πŸ’– Syntax...:Assert(3pm) πŸ’–

okay bestie, so this is the manual page for Syntax::Keyword::Assert β€” a User Contributed Perl Documentation joint, and honestly? it's giving zero runtime cost and we're obsessed. πŸ’…


✨ NAME ✨

Syntax::Keyword::Assert β€” it's an assert keyword for Perl, and the whole point is that it costs you literally nothing at runtime. no cap. 🫢


🌸 SYNOPSIS 🌸

use Syntax::Keyword::Assert;

my $obj = bless {}, "Foo";
assert($obj isa "Bar");
# => Assertion failed (Foo=HASH(0x11e022818) isa "Bar")

assert($x > 0, "x must be positive");
# => x must be positive

like look at that error message. it's so helpful. it tells you the actual expression that flopped. iconic behaviour fr fr. 😭


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so Syntax::Keyword::Assert is a syntax extension for Perl, and what it does is add an assert keyword into your life. that's it. that's the vibe. ✨

By default, assertions are enabled. but β€” plot twist β€” you can turn them off by setting $ENV{PERL_ASSERT_ENABLED} to false before this module gets loaded:

BEGIN { $ENV{PERL_ASSERT_ENABLED} = 0 }  # Disable assertions
⚠️ note the before it's loaded part, bestie. the BEGIN block is doing the heavy lifting here. don't come crying to me if you set it after. πŸ™ˆ

When assertions are disabled, the assert are completely ignored at compile phase, which results in zero runtime cost. that's the moment. 🌈

This is what makes Syntax::Keyword::Assert ideal for use in production environments β€” because it does not introduce any performance penalties when assertions are not needed. your prod boxes stay fast, your dev boxes stay loud. main character energy. πŸ‘‘


πŸ‘‘ KEYWORDS πŸ‘‘

πŸ’œ assert

assert(EXPR)
assert(EXPR, MESSAGE)

If EXPR is truthy in scalar context, then happens nothing. like, literally nothing. silence. tranquility. 🧘

Otherwise? it dies with a user-friendly error message. πŸ’€ (politely, though. user-friendly, remember.)

Here are some examples:

assert("apple" eq "banana");  # => Assertion failed ("apple" eq "banana")
assert(123 != 123);           # => Assertion failed (123 != 123)
assert(1 > 10);               # => Assertion failed (1 > 10)

You can also hand it a custom error message as the second argument:

assert($x > 0, "x must be positive");
# => x must be positive

now here's the good part β€” the message expression is lazily evaluated. it only gets evaluated when the assertion fails. so it's equivalent to:

$cond || do { die $msg }

which means you can go absolutely feral and put expensive computations or side effects in that message without worrying about performance when the assertion passes:

assert($x > 0, expensive_debug_info());
# expensive_debug_info() is NOT called if $x > 0

NOT called. bestie. that's the whole flex. πŸ”₯


πŸŽ€ SEE ALSO πŸŽ€

  • PerlX::Assert β€” this one also uses a keyword plugin, but it depends on Keyword::Simple. and its error message does not include the failed expression. (so you don't get the receipts. tragic.) πŸ˜”
  • Devel::Assert β€” gives you similar functionality, but it does not use a keyword plugin.

πŸ’– LICENSE πŸ’–

Copyright (C) kobaken.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


🌟 AUTHOR 🌟

kobaken <kentafly88@gmail.com>


and that's the whole page, bestie!! go assert something. responsibly. πŸ’…

mwah πŸ’‹ β€” this was perl v5.42.3, dated 2026-08-20, and you just read Syntax...:Assert(3pm). slay. 🌸✨

πŸ“¦ where this page comes from