2 Sub::Op - Install subroutines as opcodes.
12 STATIC OP *scalar_util_reftype(pTHX) {
19 PUSHs(sv_reftype(SvRV(sv), 0));
25 MODULE = Scalar::Util::Ops PACKAGE = Scalar::Util::Ops
31 c.len = sizeof("reftype")-1;
32 c.pp = scalar_util_reftype;
35 sub_op_register(aTHX_ &c);
38 In your Perl module file :
40 package Scalar::Util::Ops;
47 use Sub::Op; # Before loading our own shared library
52 push @ISA, 'DynaLoader';
53 __PACKAGE__->bootstrap($VERSION);
56 sub import { Sub::Op::enable(reftype => scalar caller) }
58 sub unimport { Sub::Op::disable(reftype => scalar caller) }
64 use ExtUtils::Depends;
66 my $ed = ExtUtils::Depends->new('Scalar::Util::Ops' => 'Sub::Op');
69 $ed->get_makefile_vars,
74 This module provides a C and Perl API for replacing subroutine calls by
75 custom opcodes. This has two main advantages :
77 * it gets rid of the overhead of a normal subroutine call ;
79 * there's no symbol table entry defined for the subroutine.
81 Subroutine calls with and without parenthesis are handled. Ampersand
82 calls are not replaced, and as such will still allow to call a
83 subroutine with same name defined earlier. This may or may not be
84 considered as a bug, but it gives the same semantics as Perl keywords,
85 so I believe it's reasonable.
87 When B and B::Deparse are loaded, they get automatically monkeypatched
88 so that introspecting modules like B::Concise and B::Deparse still
89 produce a valid output.
93 A typedef'd struct that configures how Sub::Op should handle a given
94 subroutine name. It has the following members :
98 The name of the subroutine you want to replace. Allowed to be
103 "name"'s length, in bytes.
107 The pp function that will be called instead of the subroutine.
108 "Perl_ppaddr_t" is a typedef'd function pointer defined by perl as :
110 typedef OP *(*Perl_ppaddr_t)(pTHX);
112 * "sub_op_check_t check"
114 An optional callback that will be called each time a call to "name"
115 is replaced. You can use it to attach extra info to those ops (e.g.
116 with a pointer table) or to perform more optimizations to the
117 optree. "sub_op_check_t" is a typedef'd function pointer defined by
120 typedef OP *(*sub_op_check_t)(pTHX_ OP *, void *);
124 An optional user data passed to the "check" callback.
126 "void sub_op_register(pTHX_ const sub_op_config_t *c)"
127 Registers a name and its configuration into Sub::Op. The caller is
128 responsible for allocating and freeing the "sub_op_config_t" object. No
129 pointer to it or to its members is kept.
132 "enable $name, [ $pkg ]"
133 Enable the replacement with a custom opcode of calls to the $name
134 subroutine of the $pkg package in the current lexical scope. A pp
135 callback must have been registered for $name by calling the C function
136 "sub_op_register" in the XS section of your module.
138 When $pkg is not set, it defaults to the caller package.
140 "disable $name, [ $pkg ]"
141 Disable the replacement for calls to $name in the package $pkg.
143 When $pkg is not set, it defaults to the caller package.
146 See the t/Sub-Op-LexicalSub directory that implements a complete
152 Variable::Magic, B::Hooks::EndOfScope.
159 B::Hooks::OP::Check::EntersubForCV.
162 Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
164 You can contact me by mail or on "irc.perl.org" (vincent).
167 Please report any bugs or feature requests to "bug-sub-op at
168 rt.cpan.org", or through the web interface at
169 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sub-Op>. I will be
170 notified, and then you'll automatically be notified of progress on your
171 bug as I make changes.
174 You can find documentation for this module with the perldoc command.
178 Tests code coverage report is available at
179 <http://www.profvince.com/perl/cover/Sub-Op>.
182 Copyright 2010 Vincent Pit, all rights reserved.
184 This program is free software; you can redistribute it and/or modify it
185 under the same terms as Perl itself.