Neil,

From: Neil Watkiss [mailto:[EMAIL PROTECTED]]
> 
> I'm working on a module that will allow you to directly 
> support C and C++ structs in Perl. It is not an Inline
> Language. It will allow you to bind C types directly into
> perl, something like this (although this isn't complete):

I'm glad someone else has been working on this. I've been working out how
something similar might be accomplished to extend Class::Struct with the new
semantics in Perl 6 RFC 128 in conjunction with Inline. Unfortunately this
quickly sidetracked me into the nearly impossible task of preprocessing
Perl. I like your more realistic ideas which start on the C/C++ side of
things and work their way back. 


> ----8<----
> use Inline::Struct;
> 
> my $obj = new Foo;
> $obj->a = 10;
> print $obj->a . "\n";
> 
> my $o = bless { a => 12, b => "NEILW" }, "Foo";
> 
> print $_ . "\n" for (sort keys %$obj);
> 
> __END__
> __Struct__
> 
> struct Foo {
>    int a;
>    char *b;
> };
> 
> ----8<----

Very nice. I am predisposed to favor naming the corresponding package the
same as the struct. But there are currently 377 base package names allocated
in CPAN. Surely this would result in naming conflicts and maintenance
overhead.

There is however no base packages named: I, Struct, Type

How about the above __Struct__ section producing a class Struct::Foo by
default?

my $obj = Struct::Foo->new(10, "NEILW");


> use Inline::Struct;
> use Inline C => 'DATA',
>            Struct => [qw(Person)];
> 
> my $n = bless ["neil", 10, 20], "Person";
> my $j = bless { name => "jared",
>                 data => 5,
>                 some => 15}, "Person";
> introduce($n, $j);

What's going on here? coercing the array and hash references into a blessed
object reference to the underlying struct? Then modify $n and $j via the
parameter list to be references to the struct instead of anonymous array and
hash reference? Is it possible to do this without introducing conflicts with
DESTROY being called on re-blessing $n and $j?

> __END__
> __Struct__
> 
> typedef struct {
>    char *name;
>    int some;
>    long data;
> } Person;
> 
> __C__
> 
> void introduce(Person *first, Person *second) {
>    printf("%s says `Hi, %s!'\n", first->name, second->name);
>    printf("%s replies `Nice to meet you, %s.\n", second->name,
>           first->name);
> }
> 
> ----8<----
> 
> A (growing) summary of proposed features:
>  o binding to C structures from Perl

Something based on Class::Struct with some deference to Perl 6 RFC 128 would
be nice. Here is a fabricated example:

package main;
my $o = Book->new('Moby Dick',
                  4.45,
                  Struct::Name->new(fname => 'Herman',
                                    lname => 'Melville'),
                  {bob => "It's great!"},
                  Business::ISBN->new('0553213113'));

print $o->as_string, "\n";

package Book;
use Inline::Struct;

struct( title      => 'char*',           # Inline base types
        price      => 'float',           # Inline typemaps
        author     => 'Struct::Name',    # Inline structs
        references => '%',               # perl base types
        isbn       => 'Business::ISBN'); # perl objects                 

sub as_string {
  my $book = shift;
  sprintf(qq|"%s", by %s, ISBN: %s, price: %.2f|,
          $book->title,
          $book->author,
          $book->isbn->as_string,
          $book->price)
} 


>  o each structure creates a corresponding Perl package

Per above, I suggest such packages be prefixed 'Struct::' or some such by
default.


>  o individual elements of the struct can be directly modified
>    in Perl space, without having to go through accessor
>    functions in C.

I'm not sure I follow here. If the underlying data is held in a C struct.
Wouldn't all accessors have to go through the Inline C glue? Or are you
referring to the <C>introduce</C> syntax?


I'm the maintainer of Damian Conway's Class::Contract. That doesn't
automatically qualify me as an OOPerl guru, but I'm fairly competent in that
regard. I'd be glad to help out with coding the interface to allow binding
to C structures from Perl.

Reply via email to