autodie

 view release on metacpan or  search on metacpan

lib/autodie/Scope/Guard.pm  view on Meta::CPAN


__END__

=head1 NAME

autodie::Scope::Guard - Wrapper class for calling subs at end of scope

=head1 SYNOPSIS

    use autodie::Scope::Guard;
    $^H{'my-key'} = autodie::Scope::Guard->new(sub {
        print "Hallo world\n";
    });

=head1 DESCRIPTION

This class is used to bless perl subs so that they are invoked when
they are destroyed.  This is mostly useful for ensuring the code is
invoked at end of scope.  This module is not a part of autodie's
public API.

lib/autodie/Scope/GuardStack.pm  view on Meta::CPAN

sub new {
    my ($class) = @_;

    return bless([], $class);
}

sub push_hook {
    my ($self, $hook) = @_;
    my $h_key = $H_KEY_STEM . ($COUNTER++);
    my $size = @{$self};
    $^H{$h_key} = autodie::Scope::Guard->new(sub {
        # Pop the stack until we reach the right size
        # - this may seem weird, but it is to avoid relying
        #   on "destruction order" of keys in %^H.
        #
        # Example:
        #  {
        #     use autodie;  # hook 1
        #     no autodie;   # hook 2
        #     use autodie;  # hook 3
        #  }



( run in 0.441 second using v1.01-cache-2.11-cpan-49f99fa48dc )