how can I access an array defined in another module

Discussion in 'Perl Misc' started by vabby, Jan 2, 2007.

  1. vabby

    vabby Guest

    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
    vabby, Jan 2, 2007
    #1
    1. Advertising

  2. vabby

    Rod MacBan Guest

    > 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.
    Rod MacBan, Jan 2, 2007
    #2
    1. Advertising

  3. vabby

    -berlin.de Guest

    vabby <> 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
    -berlin.de, Jan 2, 2007
    #3
  4. vabby

    vabby Guest

    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;
    }
    }
    vabby, Jan 2, 2007
    #4
  5. 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


    --

    Windows Vista and your freedom in conflict:
    http://www.badvista.org/
    Mumia W. (on aioe), Jan 2, 2007
    #5
  6. vabby

    Rod MacBan Guest

    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.
    Rod MacBan, Jan 2, 2007
    #6
  7. vabby

    vabby Guest

    thnx, both the solns wrk,

    Vabby
    vabby, Jan 2, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,735
    Keith Thompson
    Sep 27, 2005
  2. Replies:
    6
    Views:
    376
  3. abcd
    Replies:
    4
    Views:
    229
    Ben Finney
    Mar 20, 2007
  4. Replies:
    6
    Views:
    342
    Eric Sosman
    Oct 24, 2012
  5. Christopher Pisz
    Replies:
    6
    Views:
    223
    Anand Hariharan
    Apr 29, 2013
Loading...

Share This Page