B-Hooks-OP-Check-EntersubForCV

 view release on metacpan or  search on metacpan

lib/B/Hooks/OP/Check/EntersubForCV.pm  view on Meta::CPAN

use strict;
use warnings;

package B::Hooks::OP::Check::EntersubForCV;

use parent qw/DynaLoader/;
use B::Hooks::OP::Check 0.19;
use Scalar::Util qw/refaddr/;
use B::Utils 0.19 ();

our $VERSION = '0.10';

sub dl_load_flags { 0x01 }

__PACKAGE__->bootstrap($VERSION);

my %CALLBACKS;

sub import {
    my $class = shift;

    die 'odd number of arguments'
        unless @_ % 2 == 0;

    while (@_) {
        my ($cv, $cb) = (shift, shift);
        $CALLBACKS{ refaddr $cv } = register($cv, $cb);
    }

    return;
}

sub unimport {
    my $class = shift;

    unregister($_) for delete @CALLBACKS{ map { refaddr $_ } @_ };
    return;
}

1;

__END__

=head1 NAME

B::Hooks::OP::Check::EntersubForCV - Invoke callbacks on construction of entersub OPs for certain CVs

=head1 SYNOPSIS

=head2 From Perl

    sub foo {}

    use B::Hooks::OP::Check::EntersubForCV
        \&foo => sub { warn "entersub for foo() being compiled" };

    foo(); # callback is invoked when this like is compiled

    no B::Hooks::OP::Check::EntersubForCV \&foo;

    foo(); # callback isn't invoked

=head2 From C/XS

    #include "hook_op_check_entersubforcv.h"

    STATIC OP *
    my_callback (pTHX_ OP *op, CV *cv, void *user_data) {
        /* ... */
        return op;
    }

    hook_op_check_id id;

    /* register callback */
    id = hook_op_check_entersubforcv (cv, my_callback, NULL);

    /* unregister */
    hook_op_check_entersubforcv_remove (id);

=head1 DESCRIPTION

=head1 Perl API

=head2 import / register

    use B::Hooks::OP::Check::EntersubForCV
        \&code => \&handler;

    # or
    my $id = B::Hooks::OP::Check::EntersubForCV::register(\&code => \&handler);

Register C<handler> to be executed when an entersub opcode for the CV C<code>
points to is compiled.

When using C<register> an id that can be used for later removal of the handler
using C<unregister> is returned.

=head2 unimport / unregister

    no B::Hooks::OP::Check::EntersubForCV \&code;

    # or
    B::Hooks::OP::Check::EntersubForCV::unregister($id);

Stop calling the registered handler for C<code> for all entersubs after this.



( run in 1.966 second using v1.01-cache-2.11-cpan-140bd7fdf52 )