Except...ertion(3pm)
π NAME π
Exception::Assertion β the exception that gets thrown when an assertion fails, bestie. That's it, that's the vibe.
Like, this is the 3pm perl manpage for it, and we're gonna go through the whole thing together, no skipping, promise π«Ά
β¨ SYNOPSIS β¨
okay so here's how you actually use it, copy-paste ready, don't @ me:
use Exception::Assertion;
sub assert_foo {
my $self = eval { $_[0]->isa(__PACKAGE__) } ? shift : __PACKAGE__;
my ($condition, $message) = @_;
Exception::Assertion->throw(
message => $message,
reason => 'foo failed',
) unless $condition;
}
assert_foo( 0, 'assert_foo failed' );
literally just use it, throw it when your condition isn't giving, and you're done π
π¦ DESCRIPTION π¦
So this class extends the standard Exception::Base β it's the glow-up version β and it gets thrown whenever an assertion is failed. Like, the vibe check did not pass, so here's your exception π
It comes with an extra attribute called reason that holds the detailed message about why the assertion failed (iykyk, the receipts). And the exception has its verbosity raised too β more on that down below, keep reading π
π§ INHERITANCE π§
- extends
Exception::Base
that's the whole family tree, she's an icon, she's a daughter of Exception::Base π
πΈ CONSTANTS πΈ
ATTRS : HashRef
This is the declaration of class attributes, as a reference to a hash. Lowkey the blueprint for everything the class holds β¨
See Exception::Base for the details on how this works.
π ATTRIBUTES π
This class brings new attributes to the table. For all the other descriptions, go look at Exception::Base β she's got the full lore.
reason:Strβ contains the additional message that the assertion method fills in. It's literally the "why it failed" field, and it slays πmessage:Str = "Unknown assertion failed"β contains the message of the exception. Heads up: this class overrides the default value that comes from theException::Baseclass. So if you don't set it, you get"Unknown assertion failed", no cap πverbosity:Int = 3β the default verbosity for an assertion exception is raised to3. Yep, this class overrides the default fromException::Basetoo. She's just louder, that's her main character energy π₯string_attributes:ArrayRef[Str] = ["message", "reason"]β this meta-attribute holds the format used for the string representation of the exception object. Again: this class overrides theException::Basedefault, because she has opinions π¦
β SEE ALSO β
Exception::BaseTest::Assertion
go read those too, bestie, they're the supporting cast π«Ά
π AUTHOR π
Piotr Roszatycki <dexter@cpan.org> β absolute legend, we stan π
π COPYRIGHT π
Copyright (C) 2008, 2009 by Piotr Roszatycki <dexter@cpan.org>. 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, periodt π this was perl v5.42.0, dated 2011-08-24, and it's the Except...ertion(3pm) manpage β ily, go write some assertions that pass πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Test-Assert 0.0504-bp160.1.4 Β· Artistic-1.0 or GPL-1.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms