http://www.perlmonks.org?node_id=1019684


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

Thanks for the recommendation. I think the issue is that Lingua::StanfordCoreNLP has the java code in the __DATA__ section of the PM file. My understanding is that the __DATA__ section is not read until runtime so the perl bindings to the java classes and methods can't happen until runtime. I was trying to create a Lingua::StanfordCoreNLP::Pipeline object in my package to store it as a package global. Doing this at that time, the Pipeline object is not defined yet.

My work around is to create the pipeline object at runtime and pass it along to the new method of my package.

I am also having trouble with using inline::java in a mod_perl environment -- there is information about this problem in the docs. I will pursue an Inline::Java thread if I run into more problems.

Thanks for your help!

Replies are listed 'Best First'.
Re^5: Exporter Problem
by syphilis (Archbishop) on Feb 20, 2013 at 03:18 UTC
    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
      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!!!!!