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 # save the value of $@ so we can set $@ back to it in the begining of the eval
29 my ( @ret, $error, $failed );
31 # FIXME consider using local $SIG{__DIE__} to accumilate all errors. It's
32 # not perfect, but we could provide a list of additional errors for
36 # localize $@ to prevent clobbering of previous value by a successful
40 # failed will be true if the eval dies, because 1 will not be returned
45 # evaluate the try block in the correct context
48 } elsif ( defined $wantarray ) {
54 return 1; # properly set $fail to false
57 # copy $@ to $error, when we leave this scope local $@ will revert $@
58 # back to its previous value
62 # at this point $failed contains a true value if the eval died even if some
63 # destructor overwrite $@ as the eval was unwinding.
65 # if we got an error, invoke the catch block.
67 # This works like given($error), but is backwards compatible and
68 # sets $_ in the dynamic scope for the body of C<$catch>
70 return $catch->($error);
73 # in case when() was used without an explicit return, the C<for>
74 # loop will be aborted and there's no useful return value
79 # no failure, $@ is back to what it was, everything is fine
80 return $wantarray ? @ret : $ret[0];
97 Try::Tiny - minimal try/catch with proper localization of $@
101 # handle errors with a catch handler
105 warn "caught error: $_";
108 # just silence errors
115 This module provides bare bones C<try>/C<catch> statements that are designed to
116 minimize common mistakes with eval blocks, and NOTHING else.
118 This is unlike L<TryCatch> which provides a nice syntax and avoids adding
119 another call stack layer, and supports calling C<return> from the try block to
120 return from the parent subroutine. These extra features come at a cost of a few
121 dependencies, namely L<Devel::Declare> and L<Scope::Upper> which are
122 occasionally problematic, and the additional catch filtering uses L<Moose>
123 type constraints which may not be desirable either.
125 The main focus of this module is to provide simple and reliable error handling
126 for those having a hard time installing L<TryCatch>, but who still want to
127 write correct C<eval> blocks without 5 lines of boilerplate each time.
129 It's designed to work as correctly as possible in light of the various
130 pathological edge cases (see L<BACKGROUND>) and to be compatible with any style
131 of error values (simple strings, references, objects, overloaded objects, etc).
135 All functions are exported by default using L<Exporter>.
137 In the future L<Sub::Exporter> may be used to allow the keywords to be renamed,
138 but this technically does not satisfy Adam Kennedy's definition of "Tiny".
144 Takes one mandatory try subroutine and one optional catch subroutine.
146 The mandatory subroutine is evaluated in the context of an C<eval> block.
148 If no error occurred the value from the first block is returned, preserving
151 If there was an error and the second subroutine was given it will be invoked
152 with the error in C<$_> (localized) and as that block's first and only
155 Note that the error may be false, but if that happens the C<catch> block will
160 Intended to be used in the second argument position of C<try>.
162 Just returns the subroutine it was given.
174 There are a number of issues with C<eval>.
178 When you run an eval block and it succeeds, C<$@> will be cleared, potentially
179 clobbering an error that is currently being caught.
181 This causes action at a distance, clearing previous errors your caller may have
184 C<$@> must be properly localized before invoking C<eval> in order to avoid this
187 More specifically, C<$@> is clobbered at the begining of the C<eval>, which
188 also makes it impossible to capture the previous error before you die (for
189 instance when making exception objects with error stacks).
191 For this reason C<try> will actually set C<$@> to its previous value (before
192 the localization) in the begining of the C<eval> block.
194 =head2 Localizing $@ silently masks errors
196 Inside an eval block C<die> behaves sort of like:
200 return_undef_from_eval();
203 This means that if you were polite and localized C<$@> you can't die in that
204 scope, or your error will be discarded (printing "Something's wrong" instead).
206 The workaround is very ugly:
217 =head2 $@ might not be a true value
225 because due to the previous caveats it may have been unset.
227 C<$@> could also an overloaded error object that evaluates to false, but that's
228 asking for trouble anyway.
230 The classic failure mode is:
232 sub Object::DESTROY {
237 my $obj = Object->new;
246 In this case since C<Object::DESTROY> is not localizing C<$@> but still uses
247 C<eval> it will set C<$@> to C<"">.
249 The destructor is called when the stack is unwound, after C<die> sets C<$@> to
250 C<"foo at Foo.pm line 42\n">, so by the time C<if ( $@ )> is evaluated it has
251 been cleared by C<eval> in the destructor.
253 The workaround for this is even uglier than the previous ones. Even though we
254 can't save the value of C<$@> from code that doesn't localize, we can at least
255 be sure the eval was aborted due to an error:
257 my $failed = not eval {
263 This is because an C<eval> that caught a C<die> will always return a false
268 Using Perl 5.10 you can use L<perlsyn/"Switch statements">.
270 The C<catch> block is invoked in a topicalizer context (like a C<given> block),
271 but note that you can't return a useful value from C<catch> using the C<when>
272 blocks without an explicit C<return>.
274 This is somewhat similar to Perl 6's C<CATCH> blocks. You can use it to
275 concisely match errors:
280 when (/^Can't locate .*?\.pm in \@INC/) { } # ignore
290 C<try> introduces another caller stack frame. L<Sub::Uplevel> is not used. L<Carp>
291 will report this when using full stack traces. This lack of magic is considered
296 The value of C<$_> in the C<catch> block is not guaranteed to be preserved,
297 there is no safe way to ensure this if C<eval> is used unhygenically in
298 destructors. It's only guaranteeed that the C<catch> will be called.
308 Much more feature complete, more convenient semantics, but at the cost of
309 implementation complexity.
313 Automatic error throwing for builtin functions and more. Also designed to
314 work well with C<given>/C<when>.
318 A lightweight role for rolling your own exception classes.
322 Exception object implementation with a C<try> statement. Does not localize
325 =item L<Exception::Class::TryCatch>
327 Provides a C<catch> statement, but properly calling C<eval> is your
330 The C<try> keyword pushes C<$@> onto an error stack, avoiding some of the
331 issues with C<$@> but you still need to localize to prevent clobbering.
335 =head1 VERSION CONTROL
337 L<http://github.com/nothingmuch/try-tiny/>
341 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
345 Copyright (c) 2009 Yuval Kogman. All rights reserved.
346 This program is free software; you can redistribute
347 it and/or modify it under the terms of the MIT license.