From: Vincent Pit Date: Sat, 17 Apr 2010 13:31:22 +0000 (+0200) Subject: Introduce a scope guard object X-Git-Tag: v0.10~2 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=937bf27968e203fe3b6ea35150dbaee8f6b64458;p=perl%2Fmodules%2FCPANPLUS-Dist-Gentoo.git Introduce a scope guard object --- diff --git a/MANIFEST b/MANIFEST index 4e83022..e3e6223 100644 --- a/MANIFEST +++ b/MANIFEST @@ -5,6 +5,7 @@ Makefile.PL README lib/CPANPLUS/Dist/Gentoo.pm lib/CPANPLUS/Dist/Gentoo/Atom.pm +lib/CPANPLUS/Dist/Gentoo/Guard.pm lib/CPANPLUS/Dist/Gentoo/Maps.pm lib/CPANPLUS/Dist/Gentoo/Version.pm samples/g-cpanp @@ -17,6 +18,7 @@ t/20-version.t t/30-atom-new.t t/31-atom-cmp.t t/32-atom-and.t +t/40-guard.t t/91-pod.t t/92-pod-coverage.t t/95-portability-files.t diff --git a/lib/CPANPLUS/Dist/Gentoo/Guard.pm b/lib/CPANPLUS/Dist/Gentoo/Guard.pm new file mode 100644 index 0000000..87dbaa8 --- /dev/null +++ b/lib/CPANPLUS/Dist/Gentoo/Guard.pm @@ -0,0 +1,92 @@ +package CPANPLUS::Dist::Gentoo::Guard; + +use strict; +use warnings; + +=head1 NAME + +CPANPLUS::Dist::Gentoo::Guard - Scope guard object. + +=head1 VERSION + +Version 0.09 + +=cut + +our $VERSION = '0.09'; + +=head1 DESCRIPTION + +This is a scope guard object helper for L. + +=head1 METHODS + +=head2 C + +Creates a new L object that will call C<$coderef> when destroyed. + +=cut + +sub new { + my ($class, $code) = @_; + $class = ref($class) || $class; + + bless { + code => $code, + armed => 1, + }, $class; +} + +=head2 C + +Tells the object not to call the stored callback on destruction. + +=cut + +sub unarm { $_[0]->{armed} = 0 } + +=head2 C + +Calls the stored callback if the guard object is still armed. + +=cut + +sub DESTROY { + my ($self) = @_; + + $self->{code}->() if $self->{armed}; + $self->unarm; + + return; +} + +=head1 SEE ALSO + +L. + +=head1 AUTHOR + +Vincent Pit, C<< >>, L. + +You can contact me by mail or on C (vincent). + +=head1 BUGS + +Please report any bugs or feature requests to C, or through the web interface at L. +I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. + +=head1 SUPPORT + +You can find documentation for this module with the perldoc command. + + perldoc CPANPLUS::Dist::Gentoo + +=head1 COPYRIGHT & LICENSE + +Copyright 2009,2010 Vincent Pit, all rights reserved. + +This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. + +=cut + +1; # End of CPANPLUS::Dist::Gentoo::Guard diff --git a/t/40-guard.t b/t/40-guard.t new file mode 100644 index 0000000..954deab --- /dev/null +++ b/t/40-guard.t @@ -0,0 +1,36 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More tests => 9; + +use CPANPLUS::Dist::Gentoo::Guard; + +my $called = 0; +my $hook = sub { $called++ }; + +is $called, 0, 'not called yet'; +{ + my $guard = CPANPLUS::Dist::Gentoo::Guard->new($hook); + is $called, 0, 'creating the guard doesn\'t call the hook'; +} +is $called, 1, 'called at end of scope'; + +$called = 0; +is $called, 0, '$called reset'; +{ + my $guard = CPANPLUS::Dist::Gentoo::Guard->new($hook); + $guard->unarm; + is $called, 0, 'unarming the guard doesn\'t call the hook'; +} +is $called, 0, 'not called at end of scope'; + +$called = 0; +is $called, 0, '$called reset again'; +{ + my $guard = CPANPLUS::Dist::Gentoo::Guard->new($hook); + $guard->DESTROY; + is $called, 1, 'called DESTROY explicitely'; +} +is $called, 1, 'the hook was called only once';