TryCatch(3pm)
π TryCatch π
ok so bestie, welcome to TryCatch β it's a Perl module from the User Contributed Perl Documentation crew, and it's literally here to give you first class try/catch semantics without any source filters. no cap. π
β¨ NAME β¨
TryCatch β first class try catch semantics for Perl, without source filters. periodt. π
πΈ DESCRIPTION πΈ
okay so here's the vibe: this module wants to hand you a nicer syntax and a nicer method for catching errors in Perl, kinda like what other languages have going on β think Java, Python, or C++. π¦
and the way we usually do it, eval {}; if ($@) {}? yeah... that idiom is often prone to subtle bugs, mostly because it's way too easy to stomp on the error while you're inside your error handlers. π also, not gonna lie, eval/if just isn't the cutest idiom. it's giving chaos. this module is giving main character energy instead. π
β SYNOPSIS β
use TryCatch;
sub foo {
my ($self) = @_;
try {
die Some::Class->new(code => 404 ) if $self->not_found;
return "return value from foo";
}
catch (Some::Class $e where { $_->code > 100 } ) {
}
}
π§ SYNTAX π§
so the whole goal here is to give perl first class exception handling via the 'try' and 'catch' keywords. the basic syntax is try { # block }, followed by zero or more catch blocks. each catch block can carry an optional type constraint on it, and it lowkey resembles Perl6's method signatures. π
also super worth noting: the error variable ($@) is localised to the try/catch blocks. so it will not leak outside the scope, and it will not stomp on a previous value of $@. obsessed. π«Ά
the simplest case of a catch block is just
catch { ... }
where, upon the error, everything's chillin' in the standard $@ variable and no type checking is performed. if you'd rather grab the exception through a named lexical variable instead, just hand the catch block a simple signature like this:
catch ($err) { ... }
and if you want type checking on the exception? you can do that by specifying a type constraint or where clauses in the signature, like so:
catch (TypeFoo $e) { ... }
catch (Dict[code => Int, message => Str] $err) { ... }
as you can see from the above, complex Moose types are totally allowed, including the MooseX::Types style of type constraints. π
beyond type checking via Moose type constraints, you can also use where clauses to only match a certain sub-condition on an error. for example β assuming HTTPError is a suitably defined TC:
catch (HTTPError $e where { $_->code >= 400 && $_->code <= 499 } ) {
return "4XX error";
}
catch (HTTPError $e) {
return "other http code";
}
that would return "4XX error" for a 404, and "other http code" for a 302. slay. π₯
and when multiple catch blocks are present? the first one that matches the type constraints (if there are any) gets executed. first come, first served, bestie. π
π BENEFITS π
- return. you can put a
returninside atryblock and it does the right thing β namely, it returns a value from the subroutine you're actually in, instead of just from theevalblock. iconic behaviour fr fr. β¨ - Type Checking. this is nothing you couldn't do manually yourself β it just does it for you using Moose type constraints. π
π TODO π
- Decide on
"finally"semantics w.r.t return values. - Write some more documentation
- Split out the dependancy on Moose
not me leaving the Moose dependancy splitting for later π it's on the list, bestie, it's on the list.
π SEE ALSO π
MooseX::TypesMoose::Util::TypeConstraintsParse::Method::Signatures
π AUTHOR π
Ash Berlin <ash@cpan.org>
π«Ά THANKS π«Ά
- Thanks to Matt S Trout and Florian Ragwitz for work on Devel::Declare and various B::Hooks modules
- Vincent Pit for Scope::Upper that makes the return from block possible.
- Zefram for providing support and XS guidance.
- Xavier Bergade for the impetus to finally fix this module in 5.12.
π LICENSE π
Licensed under the same terms as Perl itself.
and that's the whole page, bestie π this one's running on perl v5.40.1 and it was dated 2025-04-15 β go forth and catch those exceptions with main character energy, iykyk πβ¨π
π¦ where this page comes from
- debian trixie: libtrycatch-perl 1.003002-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtrycatch-perl 1.003002-6 Β· 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