Problem with namespace in Perl

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with namespace in Perl

Anders Bo Rasmussen-2
I have a function I want in one perl-namespace which uses a object i
would like to have in another perl-namespace. I have tried the
following:

--test.i--
%module test
%{
#include "test.h"
%}

%include "test.h"


--test.h--
#include "test2.h"

A test4();
void test3(A a);


--test.cxx--
#include "test.h"

A test4()
{
  A a;
  return a;
}

void test3(A a)
{
  a.print();
}


--test2.i--
%module test2
%{
#include "test2.h"
%}

%include "test2.h"


--test2.h--
class A
{
  public:
    void print();
};


--test2.cxx--
#include "test2.h"
#include <iostream>

void A::print()
{
  std::cout << "Hallo world (A::print())\n";
}
--

I compile with these commands:
--
swig -c++ -perl5 test.i
swig -c++ -perl5 test2.i
g++ -c test.cxx test_wrap.cxx -I/usr/lib/perl5/5.8.7/i686-linux/CORE/
g++ -c test2.cxx test2_wrap.cxx -I/usr/lib/perl5/5.8.7/i686-linux/CORE/
c++ -shared test2_wrap.o test2.o -o libtest2.so
c++ -shared test_wrap.o test.o -o libtest.so -L=. -l test2

And my perl script that uses the c++-code:
--test.pl--
#!/usr/bin/perl

use test;
use test2;

use Data::Dumper;

my $a = test2::A->new();
my $b = test::test4();
print Dumper($a,$b);
$a->print();
test::test3($a);
--

The Dump of ($a,$b) gives:
$VAR1 = bless( {}, 'test2::A' );
$VAR2 = bless( do{\(my $o = 136171384)}, '_p_A' );

Which shows that the object created through the
function  in namespace test2 is different from the object created
through the constructor in namespace test.

$a->print(); works fine:
Hallo world (A::print())

While the $a can't be used as argument to the function test3 in the test
namespace:
Type error in argument 1 of test3. Expected _p_A


So from Perl there is two different classes called A. How do I make them
the same - and can it be done without only having one namespace in Perl?

I have tried this with SWIG 1.3.25 and 1.3.21.


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: Problem with namespace in Perl

Marcelo Matus
You need two things:

1.- Use the %import directive (see the manual)
2.- The last swig CVS version. There are some problems  with  %import  
+  perl in
older versions.

Marcelo
 
Anders Bo Rasmussen wrote:

>I have a function I want in one perl-namespace which uses a object i
>would like to have in another perl-namespace. I have tried the
>following:
>
>--test.i--
>%module test
>%{
>#include "test.h"
>%}
>
>%include "test.h"
>
>
>--test.h--
>#include "test2.h"
>
>A test4();
>void test3(A a);
>
>
>--test.cxx--
>#include "test.h"
>
>A test4()
>{
>  A a;
>  return a;
>}
>
>void test3(A a)
>{
>  a.print();
>}
>
>
>--test2.i--
>%module test2
>%{
>#include "test2.h"
>%}
>
>%include "test2.h"
>
>
>--test2.h--
>class A
>{
>  public:
>    void print();
>};
>
>
>--test2.cxx--
>#include "test2.h"
>#include <iostream>
>
>void A::print()
>{
>  std::cout << "Hallo world (A::print())\n";
>}
>--
>
>I compile with these commands:
>--
>swig -c++ -perl5 test.i
>swig -c++ -perl5 test2.i
>g++ -c test.cxx test_wrap.cxx -I/usr/lib/perl5/5.8.7/i686-linux/CORE/
>g++ -c test2.cxx test2_wrap.cxx -I/usr/lib/perl5/5.8.7/i686-linux/CORE/
>c++ -shared test2_wrap.o test2.o -o libtest2.so
>c++ -shared test_wrap.o test.o -o libtest.so -L=. -l test2
>
>And my perl script that uses the c++-code:
>--test.pl--
>#!/usr/bin/perl
>
>use test;
>use test2;
>
>use Data::Dumper;
>
>my $a = test2::A->new();
>my $b = test::test4();
>print Dumper($a,$b);
>$a->print();
>test::test3($a);
>--
>
>The Dump of ($a,$b) gives:
>$VAR1 = bless( {}, 'test2::A' );
>$VAR2 = bless( do{\(my $o = 136171384)}, '_p_A' );
>
>Which shows that the object created through the
>function  in namespace test2 is different from the object created
>through the constructor in namespace test.
>
>$a->print(); works fine:
>Hallo world (A::print())
>
>While the $a can't be used as argument to the function test3 in the test
>namespace:
>Type error in argument 1 of test3. Expected _p_A
>
>
>So from Perl there is two different classes called A. How do I make them
>the same - and can it be done without only having one namespace in Perl?
>
>I have tried this with SWIG 1.3.25 and 1.3.21.
>
>
>-------------------------------------------------------
>This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
>for problems?  Stop!  Download the new AJAX search engine that makes
>searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
>http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
>_______________________________________________
>Swig-user mailing list
>[hidden email]
>https://lists.sourceforge.net/lists/listinfo/swig-user
>  
>



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: Problem with namespace in Perl

Anders Bo Rasmussen-2
Thank you. It seems to work fine even with 1.3.25.

On Tue, Jan 17, 2006 at 10:18:31AM -0700, Marcelo Matus wrote:

> You need two things:
>
> 1.- Use the %import directive (see the manual)
> 2.- The last swig CVS version. There are some problems  with  %import  
> +  perl in
> older versions.
>
> Marcelo
>
> Anders Bo Rasmussen wrote:
>
> >I have a function I want in one perl-namespace which uses a object i
> >would like to have in another perl-namespace. I have tried the
> >following:
> >
> >--test.i--
> >%module test
> >%{
> >#include "test.h"
> >%}
> >
> >%include "test.h"
> >
> >
> >--test.h--
> >#include "test2.h"
> >
> >A test4();
> >void test3(A a);
> >
> >
> >--test.cxx--
> >#include "test.h"
> >
> >A test4()
> >{
> > A a;
> > return a;
> >}
> >
> >void test3(A a)
> >{
> > a.print();
> >}
> >
> >
> >--test2.i--
> >%module test2
> >%{
> >#include "test2.h"
> >%}
> >
> >%include "test2.h"
> >
> >
> >--test2.h--
> >class A
> >{
> > public:
> >   void print();
> >};
> >
> >
> >--test2.cxx--
> >#include "test2.h"
> >#include <iostream>
> >
> >void A::print()
> >{
> > std::cout << "Hallo world (A::print())\n";
> >}
> >--
> >
> >I compile with these commands:
> >--
> >swig -c++ -perl5 test.i
> >swig -c++ -perl5 test2.i
> >g++ -c test.cxx test_wrap.cxx -I/usr/lib/perl5/5.8.7/i686-linux/CORE/
> >g++ -c test2.cxx test2_wrap.cxx -I/usr/lib/perl5/5.8.7/i686-linux/CORE/
> >c++ -shared test2_wrap.o test2.o -o libtest2.so
> >c++ -shared test_wrap.o test.o -o libtest.so -L=. -l test2
> >
> >And my perl script that uses the c++-code:
> >--test.pl--
> >#!/usr/bin/perl
> >
> >use test;
> >use test2;
> >
> >use Data::Dumper;
> >
> >my $a = test2::A->new();
> >my $b = test::test4();
> >print Dumper($a,$b);
> >$a->print();
> >test::test3($a);
> >--
> >
> >The Dump of ($a,$b) gives:
> >$VAR1 = bless( {}, 'test2::A' );
> >$VAR2 = bless( do{\(my $o = 136171384)}, '_p_A' );
> >
> >Which shows that the object created through the
> >function  in namespace test2 is different from the object created
> >through the constructor in namespace test.
> >
> >$a->print(); works fine:
> >Hallo world (A::print())
> >
> >While the $a can't be used as argument to the function test3 in the test
> >namespace:
> >Type error in argument 1 of test3. Expected _p_A
> >
> >
> >So from Perl there is two different classes called A. How do I make them
> >the same - and can it be done without only having one namespace in Perl?
> >
> >I have tried this with SWIG 1.3.25 and 1.3.21.
> >
> >
> >-------------------------------------------------------
> >This SF.net email is sponsored by: Splunk Inc. Do you grep through log
> >files
> >for problems?  Stop!  Download the new AJAX search engine that makes
> >searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
> >http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
> >_______________________________________________
> >Swig-user mailing list
> >[hidden email]
> >https://lists.sourceforge.net/lists/listinfo/swig-user
> >
> >


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user