Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^5: Exporter Problem

by syphilis (Archbishop)
on Feb 20, 2013 at 03:18 UTC ( [id://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/Soldier.pm 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!

Cheers,
Rob

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 MyPackage.pm module in a test.pl file containing:
    use MyPackage ;
    then it fails with:
    Can't locate object method "new" via package "FOO::Soldier" at MyPackage.pm line 7.
    Compilation failed in require at test2.pl line 3.
    BEGIN failed--compilation aborted at test2.pl 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 Inline.pm 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 MyPackage.pm 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 !

      Cheers,
      Rob
      Worked perfectly, thanks!!!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1019697]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-19 13:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found