6 use vars qw(@EXPORT @EXPORT_OK $VERSION @ISA);
15 $VERSION = eval $VERSION;
17 @EXPORT = @EXPORT_OK = qw(try catch);
20 my ( $try, $catch ) = @_;
22 # we need to save this here, the eval block will be in scalar context due
24 my $wantarray = wantarray;
26 my ( @ret, $error, $failed );
28 # FIXME consider using local $SIG{__DIE__} to accumilate all errors. It's
29 # not perfect, but we could provide a list of additional errors for
33 # localize $@ to prevent clobbering of previous value by a successful
37 # failed will be true if the eval dies, because 1 will not be returned
41 # evaluate the try block in the correct context
44 } elsif ( defined $wantarray ) {
50 return 1; # properly set $fail to false
53 # copy $@ to $error, when we leave this scope local $@ will revert $@
54 # back to its previous value
58 # at this point $failed contains a true value if the eval died even if some
59 # destructor overwrite $@ as the eval was unwinding.
61 # if we got an error, invoke the catch block.
63 # This works like given($error), but is backwards compatible and
64 # sets $_ in the dynamic scope for the body of C<$catch>
66 return $catch->($error);
72 # no failure, $@ is back to what it was, everything is fine
73 return $wantarray ? @ret : $ret[0];
90 Try::Tiny - minimal try/catch with proper localization of $@
94 # handle errors with a catch handler
98 warn "caught error: $_";
101 # just silence errors
108 This module provides bare bones C<try>/C<catch> statements that are designed to
109 minimize common mistakes done with eval blocks (for instance assuming that
110 C<$@> is set to a true value on error, or clobbering previous values of C<$@>),
113 This is unlike L<TryCatch> which provides a nice syntax and avoids adding
114 another call stack layer, and supports calling C<return> from the try block to
115 return from the parent subroutine. These extra features come at a cost of a few
116 dependencies, namely L<Devel::Declare> and L<Scope::Upper> which are
117 occasionally problematic, and the additional catch filtering using L<Moose>
118 type constraints may not be desirable either.
120 The main focus of this module is to provide reliable but simple error handling
121 for those having a hard time installing L<TryCatch>, but who still want to
122 write correct C<eval> blocks without 5 lines of boilerplate each time.
124 It's designed to work as correctly as possible in light of the various
125 pathological edge cases (see L<BACKGROUND>) and to be compatible with any style
126 of error values (simple strings, references, objects, overloaded objects, etc).
130 All are exported by default using L<Exporter>.
132 In the future L<Sub::ExporteR> may be used to allow the keywords to be renamed,
133 but this technically does not satisfy Adam Kennedy's definition of "Tiny".
139 Takes one mandatory and one optional catch subroutine.
141 The mandatory subroutine is evaluated in the context of an C<eval> block.
143 If no error occured the value from the first block is returned.
145 If there was an error and the second subroutine was given it will be invoked
146 with the error in C<$_> (localized) and as that block's first and only
149 Note that the error may be false
153 Just retuns the subroutine it was given.
161 Intended to be used in the second argument position of C<try>.
167 There are a number of issues with C<eval>.
171 When you run an eval block and it succeeds, C<$@> will be cleared, potentially
172 cloberring an error that is currently being caught.
174 C<$@> must be properly localized before invoking C<eval> in order to avoid this issue.
176 =head2 Localizing $@ silently masks errors
178 Inside an eval block C<die> behaves sort of like:
182 return_undef_from_eval();
185 This means that if you were polite and localized C<$@> you can't die in that
186 scope while propagating your error.
188 The workaround is very ugly:
199 =head2 $@ might not be a true value
207 because due to the previous caveats it may have been unset. $@ could also an
208 overloaded error object that evaluates to false, but that's asking for trouble
211 The classic failure mode is:
213 sub Object::DESTROY {
218 my $obj = Object->new;
227 In this case since C<Object::DESTROY> is not localizing C<$@> but using eval it
228 will set C<$@> to C<"">.
230 The destructor is only fired after C<die> sets C<$@> to
231 C<"foo at Foo.pm line 42\n">, so by the time C<if ( $@ )> is evaluated it has
234 The workaround for this is even uglier. Even though we can't save the value of
235 C<$@> from code that doesn't localize it but uses C<eval> in destructors, we
236 can at least be sure there was an error:
238 my $failed = not eval {
244 This is because an C<eval> that caught a C<die> will always behave like
245 C<return> with no arguments.
249 Using Perl 5.10 you can enable the C<given>/C<when> construct. The C<catch>
250 block is invoked in a topicalizer context (like a C<given> block).
252 Note that you can't return a useful value from C<catch> using the C<when>
253 blocks without an explicit C<return>.
255 This is somewhat similar to Perl 6's C<CATCH> blocks. You can use it to
256 concisely match errors:
261 when (qr/^Can't locate .*?\.pm in \@INC/) { } # ignore
271 Introduces another caller stack frame. L<Sub::Uplevel> is not used. L<Carp>
272 will report this when using full stack traces. This is considered a feature.
276 The value of C<$_> in the C<catch> block is not guaranteed to be preserved,
277 there is no safe way to ensure this if C<eval> is used unhygenically in
278 destructors. It is guaranteed that C<catch> will be called, though.
288 Much more feature complete, more convenient semantics, but at the cost of
289 implementation complexity.
293 Exception object implementation with a C<try> statement. Does not localize
296 =item L<Exception::Class::TryCatch>
298 Provides a C<catch> statement, but properly calling C<eval> is your
301 The C<try> keyword pushes C<$@> onto an error stack, avoiding some of the
302 issues with C<$@> but you still need to localize to prevent clobbering.
306 =head1 VERSION CONTROL
308 L<http://github.com/nothingmuch/try-tiny/>
312 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
316 Copyright (c) 2009 Yuval Kogman. All rights reserved.
317 This program is free software; you can redistribute
318 it and/or modify it under the terms of the MIT license.