Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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.

Replies are listed 'Best First'.
Re: Exporter Problem
by syphilis (Chancellor) 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 will now produce the same Can't locate object method "new" via package ... error as is produced by loading
    Is that the case ?

    Similarly, running perl /full/path/to/ should produce the same error as running perl (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.


      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).

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 (Priest) 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 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?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2018-11-21 19:46 GMT
Find Nodes?
    Voting Booth?
    My code is most likely broken because:

    Results (249 votes). Check out past polls.