Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^5: Exporter Problem

by syphilis (Chancellor)
on Feb 20, 2013 at 03:18 UTC ( #1019697=note: print w/replies, xml ) Need Help??

in reply to Re^4: Exporter Problem
in thread Exporter Problem

I think the issue is that Lingua::StanfordCoreNLP has the java code in the __DATA__ section of the PM file

Doesn't seem to be an issue with Inline::C - though, faik, that might be quite irrelevant wrt Inline::Java.

With a site/lib/FOO/ that contains:
package FOO::Soldier; use warnings; use strict; use Inline 'C'; 1; __DATA__ __C__ typedef struct { char* name; char* rank; long serial; } Soldier; SV* new(char* class, char* name, char* rank, long serial) { Soldier* soldier; SV* obj_ref = newSViv(0); SV* obj = newSVrv(obj_ref, class); New(42, soldier, 1, Soldier); soldier->name = savepv(name); soldier->rank = savepv(rank); soldier->serial = serial; sv_setiv(obj, (IV)soldier); SvREADONLY_on(obj); return obj_ref; } char* get_name(SV* obj) { return ((Soldier*)SvIV(SvRV(obj)))->name; } char* get_rank(SV* obj) { return ((Soldier*)SvIV(SvRV(obj)))->rank; } long get_serial(SV* obj) { return ((Soldier*)SvIV(SvRV(obj)))->serial; } void DESTROY(SV* obj) { printf("Destroy called\n"); Soldier* soldier = (Soldier*)SvIV(SvRV(obj)); Safefree(soldier->name); Safefree(soldier->rank); Safefree(soldier); }
I can run the following script without any trouble:
package MyPackage; use warnings; use strict; use FOO::Soldier; my $obj1 = FOO::Soldier->new('Benjamin', 'Private', 11111); my $obj2 = FOO::Soldier->new('Sanders', 'Colonel', 22222); my $obj3 = FOO::Soldier->new('Matt', 'Sergeant', 33333); for my $obj ($obj1, $obj2, $obj3) { print $obj->get_serial, ") ", $obj->get_name, " is a ", $obj->get_rank, "\n"; }
which outputs (as expected):
11111) Benjamin is a Private 22222) Sanders is a Colonel 33333) Matt is a Sergeant Destroy called Destroy called Destroy called
Good luck with it!


Replies are listed 'Best First'.
Re^6: Exporter Problem
by Anonymous Monk on Feb 23, 2013 at 03:06 UTC
    Rob, But if you load your module in a file containing:
    use MyPackage ;
    then it fails with:
    Can't locate object method "new" via package "FOO::Soldier" at line 7.
    Compilation failed in require at line 3.
    BEGIN failed--compilation aborted at line 3.
    One or more DATA sections were not processed by Inline.
    I think this has to do with the eval'd INIT block in around line 200. I think it runs at "run" time, while all the "use" stuff runs at compile time. So it's basically not run yet. If you call:
    Inline->init() ;
    just before creating the FOO::Solfier objects it will fix the problem. Patrick
      Hi Patrick,
      Yes, I now recall this Inline->init(); trick having cropped up before - though I can't quite recall the context.

      I had the impression from earlier postings in this thread that, wrt Inline::Java, it wasn't necessary to load a to strike the problem. Instead it was enough to merely load the Inline::Java module into another namespace (as per my Inline::C demo).
      Perhaps that's not so - I don't currently have Inline::Java installed and therefore can't check that for myself.

      Anyway, if Inline::C and Inline::Java aren't in exactly the same boat here, it looks like they're at least part of the same fleet ;-)

      Hopefully Inline->init(); will solve the OP's problems.
      Thanks for chiming in, Patrick !

      Worked perfectly, thanks!!!!!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1019697]
and monks are getting baked in the sun...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2018-06-18 23:18 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.