Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Exporter Problem

by osler (Sexton)
on Feb 17, 2013 at 04:02 UTC ( #1019102=perlquestion: print w/ replies, xml ) Need Help??
osler has asked for the wisdom of the Perl Monks concerning the following question:

I am working with Lingua::StanfordCoreNLP, a module that exports subclasses when used. When I write a script I am able to use Lingua::StanfordCoreNLP and then instantiate objects that are members of the exported subclasses.

Script Code: (straight from the sample code from cpan)
# Note that Lingua::StanfordCoreNLP can't be instantiated. use Lingua::StanfordCoreNLP; # Create a new NLP pipeline (silence messages, make corefs bidirection +al) my $pipeline = new Lingua::StanfordCoreNLP::Pipeline(1, 1); # Process text # (Will output lots of debug info from the Java classes to STDERR.) my $result = $pipeline->process( $text );

When I try to use Lingua::StanfordCoreNLP in a module that I am writing, I get an error message when I try to instantiate an object member of one of the subclasses. The error message tells me that it can't find the subclass.

My Module Code:
package MyModule; use Lingua::StanfordCoreNLP; my $pipeline = new Lingua::StanfordCoreNLP::Pipeline(1, 1); sub process_text { my ($self,$text) = @_; $pipeline->process( $text ); }
Can't locate object method "new" via package "Lingua::StanfordCoreNLP::Pipeline" (perhaps you forgot to load "Lingua::StanfordCoreNLP::Pipeline"?)

I think this is a problem related to the difference between run-time and compile-time, but I can't wrap my head around why this isn't working and what I can do differently.

Comment on Exporter Problem
Select or Download Code
Re: Exporter Problem
by manorhce (Beadle) on Feb 17, 2013 at 04:12 UTC

    Hi osler welcome to Perlmonks .

    You have not shown your code you might have forgotten to load the module inside a BEGIN block and export the module. I will tell you the better way to use module is like  use base Lingua::StanfordCoreNLP;

      I tried use parent (base apparently is deprecated) and still not able to find the subclass.
Re: Exporter Problem
by frozenwithjoy (Curate) on Feb 17, 2013 at 04:12 UTC
    Can you show the code relevant to how you are trying to instantiate?
      I posted the code.
Re: Exporter Problem
by syphilis (Canon) on Feb 17, 2013 at 06:44 UTC
    Lingua::StanfordCoreNLP uses Inline::Java and I wonder if that complicates things somehow. (I don't really see how ... but I don't know much about the way Inline::Java works.)

    If you take your "Script Code" and insert package MyModule; at the very beginning of that file, then the "Script Code" is now essentially the same as "My Module Code".
    Having made that change to the "Script Code" one would therefore expect that running perl myscript.pl will now produce the same Can't locate object method "new" via package ... error as is produced by loading MyModule.pm.
    Is that the case ?

    Similarly, running perl /full/path/to/MyModule.pm should produce the same error as running perl myscript.pl (for the modified "Script Code").

    If you can test that out, it would help us determine whether Inline::Java is, in fact, the source of the problem.

    Cheers,
    Rob

      yes, when I write package at the top of the script I get the same error and when I run the pm file like a script I get the error. It appears to be related to the namespace of my package somehow breaking the creation of Lingua::StanfordCoreNLP subclasses.

      This is definitely related to Inline not Exporter which is what I originally thought. I see now that the subclasses are defined by Inline. So I need to understand how Inline exports into the calling class' namespace.
        This is definitely related to Inline not Exporter

        It's interesting that Inline::Java can have such an effect.

        Try starting another perlmonks thread under a more appropriate title. That might flush out someone who has an understanding of Inline::Java (and perhaps even a solution for you.)
        Also, you could try a post to the Inline mailing list. I think the Inline::Java author is subscribed to that mailing list, but you could cc him just in case he's not. (You'll find an email address for him here).

        Cheers,
        Rob
Re: Exporter Problem
by Anonymous Monk on Feb 17, 2013 at 14:08 UTC

    Try a singleton, try

    our $pipeline; sub pipeline { $pipeline ||= new Lingua::StanfordCoreNLP::Pipeline(1, 1); $pipeline; } sub process_text { my ($self,$text) = @_; $self->pipeline->process( $text ); }

    If things go wrong try Dumper-ing $MyModule::pipeline

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1019102]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-10-02 03:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (48 votes), past polls