Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   how can I access an array defined in another module (http://www.velocityreviews.com/forums/t901256-how-can-i-access-an-array-defined-in-another-module.html)

vabby 01-02-2007 09:41 AM

how can I access an array defined in another module
 
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


Rod MacBan 01-02-2007 10:19 AM

Re: how can I access an array defined in another module
 
> 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.


anno4000@radom.zrz.tu-berlin.de 01-02-2007 10:33 AM

Re: how can I access an array defined in another module
 
vabby <vaibhav.aparimit@gmail.com> wrote in comp.lang.perl.misc:
> 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

vabby 01-02-2007 10:50 AM

Re: how can I access an array defined in another module
 
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;
}
}


Mumia W. (on aioe) 01-02-2007 11:18 AM

Re: how can I access an array defined in another module
 
On 01/02/2007 03:41 AM, vabby wrote:
> 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


--
paduille.4060.mumia.w@earthlink.net
Windows Vista and your freedom in conflict:
http://www.badvista.org/


Rod MacBan 01-02-2007 11:33 AM

Re: how can I access an array defined in another module
 
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.


vabby 01-02-2007 11:43 AM

Re: how can I access an array defined in another module
 
thnx, both the solns wrk,

Vabby



All times are GMT. The time now is 07:18 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.