Vincent Pit > Sub-Op-0.02 > Sub::Op

Download:
Sub-Op-0.02.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

Open  1
View/Report Bugs
Module Version: 0.02   Source  

NAME ^

Sub::Op - Install subroutines as opcodes.

VERSION ^

Version 0.02

SYNOPSIS ^

In your XS file :

    #include "sub_op.h"

    STATIC OP *scalar_util_reftype(pTHX) {
     dSP;
     dMARK;
     SV *sv = POPs;
     if (SvMAGICAL(sv))
      mg_get(sv);
     if (SvROK(sv))
      PUSHs(sv_reftype(SvRV(sv), 0));
     else
      PUSHs(&PL_sv_undef);
     RETURN;
    }

    MODULE = Scalar::Util::Ops       PACKAGE = Scalar::Util::Ops

    BOOT:
    {
     sub_op_config_t c;
     c.name    = "reftype";
     c.namelen = sizeof("reftype")-1;
     c.pp      = scalar_util_reftype;
     c.check   = 0;
     c.ud      = NULL;
     sub_op_register(aTHX_ &c);
    }

In your Perl module file :

    package Scalar::Util::Ops;

    use strict;
    use warnings;

    our ($VERSION, @ISA);

    use Sub::Op; # Before loading our own shared library

    BEGIN {
     $VERSION = '0.01';
     require DynaLoader;
     push @ISA, 'DynaLoader';
     __PACKAGE__->bootstrap($VERSION);
    }

    sub import   { Sub::Op::enable(reftype => scalar caller) }

    sub unimport { Sub::Op::disable(reftype => scalar caller) }

    1;

In your Makefile.PL :

    use ExtUtils::Depends;

    my $ed = ExtUtils::Depends->new('Scalar::Util::Ops' => 'Sub::Op');

    WriteMakefile(
     $ed->get_makefile_vars,
     ...
    );

DESCRIPTION ^

This module provides a C and Perl API for replacing subroutine calls by custom opcodes. This has two main advantages :

Subroutine calls with and without parenthesis are handled. Ampersand calls are not replaced, and as such will still allow to call a subroutine with same name defined earlier. This may or may not be considered as a bug, but it gives the same semantics as Perl keywords, so I believe it's reasonable.

When B and B::Deparse are loaded, they get automatically monkeypatched so that introspecting modules like B::Concise and B::Deparse still produce a valid output.

C API ^

sub_op_config_t

A typedef'd struct that configures how Sub::Op should handle a given subroutine name. It has the following members :

void sub_op_register(pTHX_ const sub_op_config_t *c)

Registers a name and its configuration into Sub::Op. The caller is responsible for allocating and freeing the sub_op_config_t object. No pointer to it or to its members is kept.

PERL API ^

enable $name, [ $pkg ]

Enable the replacement with a custom opcode of calls to the $name subroutine of the $pkg package in the current lexical scope. A pp callback must have been registered for $name by calling the C function sub_op_register in the XS section of your module.

When $pkg is not set, it defaults to the caller package.

disable $name, [ $pkg ]

Disable the replacement for calls to $name in the package $pkg.

When $pkg is not set, it defaults to the caller package.

EXAMPLES ^

See the t/Sub-Op-LexicalSub directory that implements a complete example.

CAVEATS ^

Preexistent definitions of a sub whose name is handled by Sub::Op are restored at the end of the lexical scope in which the module is used. But if you define a sub in the scope of action of Sub::Op with a name that is currently being replaced, the new declaration will be obliterated at the scope end.

Function calls without parenthesis inside an eval STRING in the scope of the pragma won't be replaced. I know a few ways of fixing this, but I've not yet decided on which.

DEPENDENCIES ^

perl 5.10.

Variable::Magic, B::Hooks::EndOfScope.

ExtUtils::Depends.

SEE ALSO ^

subs::auto.

B::Hooks::XSUB::CallAsOp provides a C API to declare XSUBs that effectively call a specific PP function. Thus, it allows you to write XSUBs with the PP stack conventions used for implementing perl core keywords. There's no opcode replacement and no parsing hacks.

B::Hooks::OP::Check::EntersubForCV.

AUTHOR ^

Vincent Pit, <perl at profvince.com>, http://www.profvince.com.

You can contact me by mail or on irc.perl.org (vincent).

BUGS ^

Please report any bugs or feature requests to bug-sub-op at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sub-Op. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT ^

You can find documentation for this module with the perldoc command.

    perldoc Sub::Op

Tests code coverage report is available at http://www.profvince.com/perl/cover/Sub-Op.

COPYRIGHT & LICENSE ^

Copyright 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.

syntax highlighting: