how can I access an array defined in another module

V

vabby

My problem scenario is as follows.
I have a module BatLib1.pm and another module BatLib2.pm and a main
perl module mod_main.pl . Inside BatLib2.pm, there is a function fn1()
, where I declare and define an array like this:
$self->{array_test}[0] = 0;
$self->{array_test}[1] = 1;
$self->{array_test}[2] = 2;

INside mod_main.pl , I create a new instance of Batlib2,
$batlibobj2 = new_Batlib2 Batlib2();

and then a new instance of Batlib1, where I pass as arg the instance of
Batlib2
$batlibobj1 = new_Batlib1 Batlib1($batlibobj2);

Inside Batlib1.pm
I make a call to Batlib2::fn1() and it works so that the array_test
gets populated. Now I have to access this array in Batlib1. How can I
do it:

I tried using $self->{batlibobj2}->{array_test}, but it doesnot work.
Plz note that batlibobj2 is a member variable of Batlib1 which gets
intilaized in the new_Batlib1 function.

Tx
Vabby
 
R

Rod MacBan

My problem scenario is as follows.
I have a module BatLib1.pm and another module BatLib2.pm and a main
perl module mod_main.pl . Inside BatLib2.pm, there is a function fn1()
, where I declare and define an array like this:
$self->{array_test}[0] = 0;
$self->{array_test}[1] = 1;
$self->{array_test}[2] = 2;

INside mod_main.pl , I create a new instance of Batlib2,
$batlibobj2 = new_Batlib2 Batlib2();

and then a new instance of Batlib1, where I pass as arg the instance of
Batlib2
$batlibobj1 = new_Batlib1 Batlib1($batlibobj2);

Inside Batlib1.pm
I make a call to Batlib2::fn1() and it works so that the array_test
gets populated. Now I have to access this array in Batlib1. How can I
do it:

I tried using $self->{batlibobj2}->{array_test}, but it doesnot work.
Plz note that batlibobj2 is a member variable of Batlib1 which gets
intilaized in the new_Batlib1 function.

Tx
Vabby

I think you should use a $self->{batlibobj2}->fn1() call instead of a
Batlib2::fn1() call so you will initialize array of batlibobj2
instead of the Batlib2 one.

Rod.
 
A

anno4000

vabby said:
My problem scenario is as follows.
I have a module BatLib1.pm and another module BatLib2.pm and a main
perl module mod_main.pl . Inside BatLib2.pm, there is a function fn1()
, where I declare and define an array like this:
$self->{array_test}[0] = 0;
$self->{array_test}[1] = 1;
$self->{array_test}[2] = 2;

How is fn1() being called? As $obj->fn1() with $obj an instance
of BatLib2? Any other way?
INside mod_main.pl , I create a new instance of Batlib2,

"Batlib2" or "BatLib2"? Case matters.
$batlibobj2 = new_Batlib2 Batlib2();

Preferred:

$batlibobj2 = Batlib2->new_Batlib2();

Since you don't show what new_Batlib2() does you've lost us here. No
one can say what the situation is after that call.
and then a new instance of Batlib1, where I pass as arg the instance of
Batlib2
$batlibobj1 = new_Batlib1 Batlib1($batlibobj2);

Inside Batlib1.pm
I make a call to Batlib2::fn1() and it works so that the array_test
gets populated. Now I have to access this array in Batlib1. How can I
do it:

I tried using $self->{batlibobj2}->{array_test}, but it doesnot work.

"Does not work" is not an error description. Explain what you expected
it to do and how you know it does something else.

Apart from that, you're not supposed to access object variables
directly through their hash keys. There should be an accessor method
for that.
Plz note that batlibobj2 is a member variable of Batlib1 which gets
intilaized in the new_Batlib1 function.

As noted, if it's a member variable there should be an accessor
for it.

Your error description is insufficient for a diagnosis. Post a
short but complete script that demonstrates the problem, as suggested
in the posting guidelines for this group.

Anno
 
V

vabby

the following is the mod_main.pl
$batlibobj2 = new_Batlib2 Batlib2();

$batlibobj1 = new_Batlib1 Batlib1($batlibobj2);
$batlibobj1->fun1();


Inside Batlib1.pm , I have the following code written:
sub new_Batlib2 {
print "Entering new_Batlib2\n";
my ($class, $self) = (shift, {});
bless $self, $class;
return $self;
}


sub fun2
{

my $self = shift;
print "\n Now in Batlib2::fun2 fn \n";
$self->{array_test}[0] = 0;
$self->{array_test}[1] = 1;
$self->{array_test}[2] = 2;

}


Inside BatLib1.pm , I have the following code written:
sub new_Batlib1 {
print "Entering new_Batlib1\n";
my @args = qw(batlibobj2);
my ($class, $self) = (shift, {});
%$self = map { $_, shift } @args;
bless $self, $class;
return $self;

}

sub fun1
{
my $self = shift;
print "\n Now in batlib1::fun1 fn ";
$self->{batlibobj2}->fun2();
foreach my $var ($self->{batlibobj2}->{array_test})
{
print $var;
}
}
 
M

Mumia W. (on aioe)

My problem scenario is as follows.
I have a module BatLib1.pm and another module BatLib2.pm and a main
perl module mod_main.pl . Inside BatLib2.pm, there is a function fn1()
, where I declare and define an array like this:
$self->{array_test}[0] = 0;
$self->{array_test}[1] = 1;
$self->{array_test}[2] = 2;

INside mod_main.pl , I create a new instance of Batlib2,
$batlibobj2 = new_Batlib2 Batlib2();

and then a new instance of Batlib1, where I pass as arg the instance of
Batlib2
$batlibobj1 = new_Batlib1 Batlib1($batlibobj2);

Inside Batlib1.pm
I make a call to Batlib2::fn1() and it works so that the array_test
gets populated. Now I have to access this array in Batlib1. How can I
do it:

I tried using $self->{batlibobj2}->{array_test}, but it doesnot work.
Plz note that batlibobj2 is a member variable of Batlib1 which gets
intilaized in the new_Batlib1 function.

Tx
Vabby

Create a method in Batlib2 that returns the value of array_test and let
Batlib1 use that value to access the array.

(I'm really bored right now ;-)

---------------cut----------------
#!/usr/bin/perl
use strict;
use warnings;
use Batlib1;
use Batlib2;

my $batlibobj2 = new Batlib2;
my $batlibobj1 = new Batlib1($batlibobj2);
$batlibobj1->show_test;
--------------cut-------------------
package Batlib1;

sub new {
my $class = shift;
my $self = { batlibobj2 => shift() };
$self->{batlibobj2}->fn1();
bless ($self, $class);
}

sub show_test {
my $self = shift;
my $at = $self->{batlibobj2}->array_test();
print "Array test element: $_\n" for (@$at);
}


1;
----------------cut------------
package Batlib2;

sub new {
my $class = shift;
bless ({}, $class);
}

sub fn1 {
my $self = shift;
$self->{array_test}[0] = 0;
$self->{array_test}[1] = 1;
$self->{array_test}[2] = 2;
$self;
}

sub array_test {
my $self = shift;
$self->{array_test} = $_[0] if @_;
$self->{array_test};
}

1;
---------------cut-------------

HTH
 
R

Rod MacBan

Ah ok; somehow you have to cast the array_test before you can
dereference it, like that

foreach my $var ( @{$self->{batlibobj2}->{array_test}} )

Rod.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top