--- Guard/Guard.pm 2008/12/13 17:50:29 1.3 +++ Guard/Guard.pm 2008/12/15 02:38:21 1.16 @@ -4,7 +4,16 @@ =head1 SYNOPSIS - use Guard; + use Guard; + + # temporarily chdir to "/etc" directory, but make sure + # to go back to "/" no matter how myfun exits: + sub myfun { + scope_guard { chdir "/" }; + chdir "/etc"; + + code_that_might_die_or_does_other_fun_stuff; + } =head1 DESCRIPTION @@ -27,8 +36,10 @@ package Guard; +no warnings; + BEGIN { - $VERSION = '0.1'; + $VERSION = '0.5'; @ISA = qw(Exporter); @EXPORT = qw(guard scope_guard); @@ -45,11 +56,14 @@ Registers a block that is executed when the current scope (block, function, method, eval etc.) is exited. +See the EXCEPTIONS section for an explanation of how exceptions +(i.e. C) are handled inside guard blocks. + The description below sounds a bit complicated, but that's just because C tries to get even corner cases "right": the goal is to provide you with a rock solid clean up tool. -This is similar to this code fragment: +The behaviour is similar to this code fragment: eval ... code following scope_guard ... { @@ -62,17 +76,26 @@ Except it is much faster, and the whole thing gets executed even when the BLOCK calls C, C, C or escapes via other means. -See B, below, for an explanation of exception handling -(C) within guard blocks. - -Example: Temporarily change the directory to F and make sure it's -set back to F when the function returns: - - sub dosomething { - scope_guard { chdir "/" }; - chdir "/etc"; +If multiple BLOCKs are registered to the same scope, they will be executed +in reverse order. Other scope-related things such as C are managed +via the same mechanism, so variables Cised I calling +C will be restored when the guard runs. + +Example: temporarily change the timezone for the current process, +ensuring it will be reset when the C scope is exited: + + use Guard; + use POSIX (); + + if ($need_to_switch_tz) { + # make sure we call tzset after $ENV{TZ} has been restored + scope_guard { POSIX::tzset }; + + # localise after the scope_guard, so it gets undone in time + local $ENV{TZ} = "Europe/London"; + POSIX::tzset; - ... + # do something with the new timezone } =item my $guard = guard BLOCK @@ -84,18 +107,21 @@ The returned object can be copied as many times as you want. -See B, below, for an explanation of exception handling -(C) within guard blocks. +See the EXCEPTIONS section for an explanation of how exceptions +(i.e. C) are handled inside guard blocks. Example: acquire a Coro::Semaphore for a second by registering a -timer. The timer callback references the guard used to unlock it again. +timer. The timer callback references the guard used to unlock it +again. (Please ignore the fact that C has a C +method that does this already): + use Guard; use AnyEvent; use Coro::Semaphore; my $sem = new Coro::Semaphore; - sub lock_1s { + sub lock_for_a_second { $sem->down; my $guard = guard { $sem->up }; @@ -111,9 +137,11 @@ $sem->down >> in the callback is that you can opt not to create the timer, or your code can throw an exception before it can create the timer, or you can create multiple timers or other event watchers and only when the last -one gets executed will the lock be unlocked. +one gets executed will the lock be unlocked. Using the C, you do +not have to worry about catching all the places where you have to unlock +the semaphore. -=item Guard::cancel $guard +=item $guard->cancel Calling this function will "disable" the guard object returned by the C function, i.e. it will free the BLOCK originally passed to @@ -130,26 +158,29 @@ =head1 EXCEPTIONS -Guard blocks should not normally throw exceptions (e.g. C), after +Guard blocks should not normally throw exceptions (that is, C). After all, they are usually used to clean up after such exceptions. However, if something truly exceptional is happening, a guard block should be allowed to die. Also, programming errors are a large source of exceptions, and the programmer certainly wants to know about those. -Since in most cases, the block executing when the guard gets executes does +Since in most cases, the block executing when the guard gets executed does not know or does not care about the guard blocks, it makes little sense to let containing code handle the exception. Therefore, whenever a guard block throws an exception, it will be caught, -and this module will call the code reference stored in C<$Guard::DIED> -(with C<$@> set to the actual exception), which is similar to how most -event loops handle this case. +followed by calling the code reference stored in C<$Guard::DIED> (with +C<$@> set to the actual exception), which is similar to how most event +loops handle this case. + +The default for C<$Guard::DIED> is to call C. + +The C<$@> variable will be restored to its value before the guard call in +all cases, so guards will not disturb C<$@> in any way. The code reference stored in C<$Guard::DIED> should not die (behaviour is not guaranteed, but right now, the exception will simply be ignored). -The default for C<$Guard::DIED> is to call C. - =head1 AUTHOR Marc Lehmann @@ -157,8 +188,17 @@ =head1 THANKS -To Marco Maisenhelder, who reminded me of the C<$Guard::DIED> solution to -the problem of exceptions. +Thanks to Marco Maisenhelder, who reminded me of the C<$Guard::DIED> +solution to the problem of exceptions. + +=head1 SEE ALSO + +L and L, which actually implement +dynamic, not scoped guards, and have a lot higher CPU, memory and typing +overhead. + +L, which has apparently never been finished and corrupts +memory when used. =cut