A few correction/additions:
- Of course for Perl you do not need to call Inline. Just have the
sub{} do whatever it takes to load your code.
- Calc::Calc in Calc.cpp should be more like this:
Calc::Calc(){
}
Thanks,
Patrick
25 Jan 2005 15:37:26 -0500, Patrick LeBoutillier
<[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I have a Perl client for a project I'm working on these days. The API
> for the client is really simple (1 class with new(), connect(),
> read(), print() and close() methods). Naturally I have
> a Perl test suite written using the standard (Test.pm in my case) tools.
>
> Once my project was pretty much working, I realized I would need a
> Java client, so I wrote one, using the same exact API.
>
> It was then that I realized that, with a few lines of Perl code and
> Inline::Java, I could use the Perl test suite I wrote for the Perl
> client to test the Java Client.
>
> I then realized that I could push it further and started writing a new
> module, Inline::Select. It works like this:
>
> Suppose you have the following code, all in different languages:
>
> Calc.cpp:
> class Calc {
> public:
> Calc() ;
> int add(int a, int b) ;
> } ;
> Calc::Calc(){
> lang = strdup("CPP") ;
> }
> int Calc::add(int a, int b){
> return a + b ;
> }
>
> Calc.java:
> class Calc {
> public Calc(){
> }
> public int add(int a, int b){
> return a + b ;
> }
> }
>
> Calc.pm:
> package Calc ;
> sub new {
> my $class = shift ;
> bless({}, $class) ;
> }
> sub add {
> my $this = shift ;
> my $a = shift ;
> my $b = shift ;
> return $a + $b ;
> }
> 1 ;
>
> Calc.py:
> class Calc:
> def __init__(self) :
> pass
>
> def add(self,a,b) :
> return a + b
>
> You could then write a test script like this:
>
> use strict ;
> use warnings ;
> use Test ;
>
> BEGIN {plan(tests => 1) ;}
>
> use Inline::Select (
> PACKAGE => 'Calc',
> Inline => [ Perl => sub {require 't/Calc.pm'} ]
> ) ;
> use Inline::Select (
> PACKAGE => 'Calc',
> Inline => [ CPP => 't/Calc.cpp' ]
> ) ;
> use Inline::Select (
> PACKAGE => 'Calc',
> Inline => [ Java => 't/Calc.java' ]
> ) ;
> use Inline::Select (
> PACKAGE => 'Calc',
> Inline => [ Python => 't/Calc.py' ]
> ) ;
>
> use Inline::Select (
> PACKAGE => 'Calc',
> Inline => $ARGV[0]
> ) ;
>
> my $c = new Calc() ;
> ok($c->add(2, 3), 5) ;
>
> And then say:
>
> $ test.pl (Perl|Java|CPP|Python)
>
> The 'Calc' namespace will then be "filled in" by the approriate Inline
> language block and the script will automatically pick up the right
> implementation.
>
> The way it works is easy: Each time you "use Inline::Select" with an
> array ref as the Inline parameter, the block is saved for later use.
> When the Inline parameter is a scalar, that language is selected and
> the appropriate Inline block is compiled in the caller package. The
> PACKAGE parameter is used to identify which blocks go together.
>
> I'm still not sure the API is optimal but, hey, its still version 0.01...
>
> Anyone have any comments/suggestions/anything?
>
> Cheers,
>
> Patrick
>
> --
> =====================
> Patrick LeBoutllier
> Laval, Qu�bec, Canada
>
--
=====================
Patrick LeBoutllier
Laval, Qu�bec, Canada