Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Inline Java in a Perl OO Module

by tmaly (Monk)
on Nov 21, 2008 at 05:48 UTC ( [id://725057]=perlquestion: print w/replies, xml ) Need Help??

tmaly has asked for the wisdom of the Perl Monks concerning the following question:

I was able to get Inline::Java working from withing a perl OO module today. However, I was hoping to be able to put all of the inline code in one method and then be able to instantiate objects from this one instance of the inline code throughout the perl module.

However, attempting to do this did not seem to work, and I was not able to persist the inline java to the rest of the perl object. I turned on the debug mode in inline and I saw that it was adding the java class to the namespace of the perl module as I was hoping for. But I wanted to avoid having to replicate the inline code in other methods throughout this module. I suspect the problem is related to lexical scoping and how inline java works. Has anyone accomplished anything like this before?



Here is the pseudo code of what I hope to accomplish
package ABC; sub new { .... } sub init { my $self = shift; use Inline Java << EOJ , .... .... EOJ ; $self->{obj} = ABC::javasclassfrominline->new() } sub somemethod { my $self = shift; $self->{obj}->callsomejavamethod(); } 1;

Update: Thanks guys, I ended up putting the code outside as fmerges suggested. I stuck it in a second BEGIN block and I was able to call it as I was hoping to.

Replies are listed 'Best First'.
Re: Inline Java in a Perl OO Module
by fmerges (Chaplain) on Nov 21, 2008 at 06:16 UTC

    Hi,

    Put the java code outside, and use the STUDY option. Example from the manual:

    use Inline ( Java => 'STUDY', STUDY => ['java.util.HashMap'], );

    If it's not enough like this, you can also make a wrapper Class, and use it from ABC Class.

    Regards,

    fmerges at irc.freenode.net
Re: Inline Java in a Perl OO Module
by ikegami (Patriarch) on Nov 21, 2008 at 07:31 UTC
    I don't know what you are trying to achieve or how to achieve it, but I do spot something wrong.

    use is executed as soon as it's compiled, but your placement of the statement suggests you think it will be executed when init is called, but that's not the case. It will only be executed once, and it will be executed whether init is called or not. The following code is equivalent to yours:

    package ABC; use Inline Java << EOJ , .... .... EOJ ; sub new { .... } sub init { my $self = shift; $self->{obj} = ABC::javasclassfrominline->new() } sub somemethod { my $self = shift; $self->{obj}->callsomejavamethod(); } 1;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-29 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found