Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

chromatic (983)

chromatic
  (email not shown publicly)
http://wgz.org/chromatic/

Blog Information [technorati.com] Profile for chr0matic [technorati.com]

Journal of chromatic (983)

Wednesday May 10, 2006
06:42 PM

More NCI Fun

[ #29583 ]

Did you know that Perl 5 subroutine attributes can apply to forward declarations even if there's no full declaration later? I thought this was true, but I had to make sure.

You can get rid of a lot of unnecessary code with these tricks. (I like Attribute::Handlers a lot.)

use Test::More tests => 1;
use P5NCI::Attribute library => 'nci_test', path => 'src';

sub double_int :NCI( double_int => ii );

is( double_int( 10 ), 20, 'NCI attribute should install named thunk' );

I don't know if general purpose NCI can get much shorter than this, but I like it.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • You know, I never thought of applying attributes to forward declarations. You've gotten me thinking. That's probably a bad thing ... :)

  • Oh dear, what an amazingly cool trick.

    You've got me thinking as well.

    But first I'd like to find out if that trick is a) Expected b) Going to keep working c) Back-compatible for some reasonable length (5.6.1 being probably the best target to even consider it).

  • use P5NCI::Attribute library => 'nci_test', path => 'src';

    sub double_int :NCI( double_int => ii );


    versus


    use P5NCI::Easy # *cringe*
            library => 'nci_test',
            path => 'src',
            connect => [ 'double_int:ii' ];


    or perhaps


    use P5NCI::Simple nci_test => 'src', {
            double_int => 'ii',
            };


    Or maybe some combination or variant.

    Have a look at the import syntax for [cpan.org]
    • If only want to install a thunk as the name of the bound function, those all work too. This example doesn't really show that using different names here is trivial.

      I do think this works on Perl 5.6.1. At least, the only thing I worry about is the attribute trick. (See the first code example on page 232 of Camel 3 for why I don't worry much.)