refer to object in another package

Discussion in 'Perl Misc' started by ed, Sep 29, 2003.

  1. ed

    ed Guest

    Hey all. This is probably a simple question, but I've looked around
    the faq and other parts of the manual and haven't found an answer yet.

    I want to use an object from another package.

    Example:

    --- START Perl CODE -----

    $obj = new Object;

    # ... DO STUFF in anotherPackage ......


    { package anotherPackage;

    # How do I refer to $obj from here?
    # I've tried $main::eek:bj and *main::eek:bj
    # but neither seem to work

    }


    --- END Perl CODE ------

    tia,
    --ed
     
    ed, Sep 29, 2003
    #1
    1. Advertising

  2. <snipped>
    > I want to use an object from another package.
    > { package anotherPackage;

    <Snipped>
    > # I've tried $main::eek:bj and *main::eek:bj
    > # but neither seem to work}


    try : $newobject = Package::Classname->new();

    HTH
    Michael
     
    Michael Korte, Sep 29, 2003
    #2
    1. Advertising

  3. ed

    ed Guest

    On Mon, 29 Sep 2003 09:15:13 +0200, "Michael Korte"
    <> wrote:

    ><snipped>
    >> I want to use an object from another package.
    >> { package anotherPackage;

    ><Snipped>
    >> # I've tried $main::eek:bj and *main::eek:bj
    >> # but neither seem to work}

    >
    >try : $newobject = Package::Classname->new();
    >
    >HTH
    >Michael
    >



    Thanks for the reply. But I would like to use an object that's
    already been instantiated in another package.

    For now I can do something like this:


    package main;

    my $obj = new className;
    &AnotherPackage::sendObj($obj);


    { package AnotherPackage;

    my $obj;

    sub sendObj
    { $obj = shift;
    }

    }


    --ed
     
    ed, Sep 29, 2003
    #3
  4. ed

    Jay Tilton Guest

    ed <> wrote:

    : Hey all. This is probably a simple question, but I've looked around
    : the faq and other parts of the manual and haven't found an answer yet.
    :
    : I want to use an object from another package.
    :
    : --- START Perl CODE -----
    : $obj = new Object;
    :
    : # ... DO STUFF in anotherPackage ......
    :
    : { package anotherPackage;
    : # How do I refer to $obj from here?
    : # I've tried $main::eek:bj and *main::eek:bj
    : # but neither seem to work
    : }
    : --- END Perl CODE ------

    $obj is just a scalar. That it is an object has no impact on its scope
    or how to access it from other packages.

    Unless $obj is declared with my(), there's no problem with using it as
    $main::eek:bj .

    If $obj is declared with my(), and the anotherPackage package is in the
    same file, as it is in the sample code, there's no problem using it at
    all--its scope extends to the end of the file.

    If $obj is was declared with my(), and anotherPackage is held in an
    unrelated scope, you're boned.
     
    Jay Tilton, Sep 30, 2003
    #4
  5. ed

    ed Guest

    On Mon, 29 Sep 2003 23:12:46 GMT, (Jay Tilton)
    wrote:

    >Unless $obj is declared with my(), there's no problem with using it as
    >$main::eek:bj .
    >
    >If $obj is declared with my(), and the anotherPackage package is in the
    >same file, as it is in the sample code, there's no problem using it at
    >all--its scope extends to the end of the file.



    $obj is delcared with my() in the actual script I'm working with. My
    example doesn't refect that. Sorry about that.
    And it is being used in the same file, so I don't guess I need to use
    any qualified name to access it.

    Thanks a lot.

    --ed
     
    ed, Sep 30, 2003
    #5
    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. Tor Inge Rislaa

    How to refer to a variable on another webform

    Tor Inge Rislaa, Aug 5, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    500
    S. Justin Gengo
    Aug 5, 2003
  2. Adrian
    Replies:
    1
    Views:
    395
    Eliyahu Goldin
    Mar 12, 2006
  3. db
    Replies:
    2
    Views:
    718
    Mitja
    Feb 5, 2004
  4. Parzival
    Replies:
    1
    Views:
    337
    Peter L Hansen
    Oct 9, 2004
  5. Gee Chen
    Replies:
    2
    Views:
    446
    Gee Chen
    Aug 29, 2011
Loading...

Share This Page