Hi,

just playing around with Damian's Attribute::Handlers; here's
*the beginning of* an idea to inline subs directly, using attributes:


#!/usr/bin/perl

use warnings;
use strict;
use lib './lib';
use Attribute::Inline;

sub add :C('int', 'int x, int y') {
         qq{ return x + y; }
}

sub subtract :C('int', 'int x, int y') {
         qq{ return x - y; }
}

print "9 + 16 = ", add(9, 16), "\n";
print "9 - 16 = ", subtract(9, 16), "\n";


and here is Attribute::Inline (note that there is no error checking etc.)
- this is just a proof of concept:


package Attribute::Inline;

use warnings;
use strict;
use Attribute::Handlers;

sub UNIVERSAL::C :ATTR(CODE) {
         my ($pkg, $sym, $ref, $attr, $data) = @_;
         $data = [ $data ] unless ref $data eq 'ARRAY';
         my ($returns, $args) = @$data;
         (my $name = $sym) =~ s/.*:://;
         my $body = $ref->();
         my $code = qq!
                 package $pkg;
                 use Inline $attr => "$returns $name($args) { $body }";
         !;
         eval $code;
         die "Internal error: $@" if $@;
}

1;


I'd appreciate comments and ideas; maybe there is a way of making
the sub declaration more pretty. The sub declaration is easier
for loosely typed languages such as Python. Also, if you have another
idea as to how to pass the code to Inline apart from wrapping it in
a string, I'd be interested to hear that. And maybe 'Attribute::Inline'
isn't the best name for it.

Marcel

--
my int ($x, $y, $z, $n); $x**$n + $y**$n = $z**$n is insoluble if $n > 2;
I have discovered a truly remarkable proof which this signature is too
short to contain.      (20 Aug 2001: Pierre de Fermat's 400th birthday)

Reply via email to