The problem:
I instantiate a number of Java class instances which wrap a Perl class and stick them in an array. When I loop through the array, calling a function which should return a different value for each one, instead I get back the same (last set) value for all of them.
I've reduced to a test case.
The output is:
New instance with value 0 <--------------creating a new instance
AnInstance: 0 <--------------- value passed to constructor in AnInstance
value: 0 <--------------- call to Value() function inside AnInstance constructor
==Value: 0 <---------------- call to Value() function on AnInstance instance outside constructor
New instance with value 1
AnInstance: 1
value: 1
==Value: 1
New instance with value 2
AnInstance: 2
value: 2
==Value: 2
0: 2 <---------------- call to Value() function on AnInstance instance in successive loop
1: 2
2: 2
It looks like everything is going in correctly, but when I iterate through the array and check the values, they are all the same.
Please tell me I'm doing some idiotic newbie Java programmer funkiness.
Here's some code:
TestClassInstance.java ==================================== import java.util.*; import org.perl.inline.java.*;
public class TestClassInstances extends InlineJavaPerlCaller { static private InlineJavaPerlInterpreter pi = null;
public TestClassInstances() throws InlineJavaException { }
public static void main(String argv[]) throws InlineJavaPerlException, InlineJavaException
{
pi = InlineJavaPerlInterpreter.create();
pi.eval("use lib qw(/home/aaron/lib);");
pi.require_module("TestClassInstance");
AnInstance[] aInstances = new AnInstance[3];
for(int i = 0; i < 3; i++)
{
System.out.println("New instance with value " + i);
AnInstance inst = new AnInstance(pi, new Integer(i));
System.out.println("==Value: " + inst.Value());
aInstances[i] = inst;
}
for(int x = 0; x < aInstances.length; x++)
{
AnInstance inst = aInstances[x];
System.out.println(x + ": " + inst.Value());
}
pi.destroy(); }
}; ===========================================
AnInstance.java =========================================== import java.util.*; import org.perl.inline.java.*;
public class AnInstance { static private InlineJavaPerlObject inst = null;
public AnInstance(InlineJavaPerlInterpreter pi, Integer intIn) throws InlineJavaPerlException, InlineJavaException
{
System.out.println("AnInstance: " + intIn.toString());
inst = (InlineJavaPerlObject) pi.CallPerlSub("TestClassInstance::new", new Object [] { intIn }, InlineJavaPerlObject.class);
System.out.println(" value: " + Value());
}
public String Value() throws InlineJavaPerlException, InlineJavaException
{
String strReturn = (String) inst.InvokeMethod("Value", new Object [] {}, String.class);
return strReturn;
}
}; =============================================
TestClassInstance.pm ============================================= package TestClassInstance;
use strict; use warnings;
sub new { my($sValue) = @_; my $self = { Value => $sValue }; bless($self, "TestClassInstance"); return $self; }
sub Value { my($self, $sNewValue) = @_; $self->{Value} = $sNewValue if($sNewValue);
return $self->{Value}; }
1; =============================================== --
Aaron Craig
[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> msn: maremma_mercutio
=-=-=-=
The end crowns all, and that old common arbitrator, time, will one day end it -- Troilus & C, Act iv, Sc.5
=-=-=-=