Passing a Module as an Object or Reference

Discussion in 'Perl Misc' started by Hal Vaughan, Aug 23, 2005.

  1. Hal Vaughan

    Hal Vaughan Guest

    I have a module, TD::Log.pm (TD is the company name, so most modules are in
    that directory). When I write a program, I use:

    use TD::Log;

    as expected. I also have other modules I use. I know a module can
    reference the function "entry()" in the instance TD::Log in the main
    program by using:

    main::TD::Log::entry("status: adding today's data to file");

    However, what I'd like to do, when I open a new module from main, say
    "TD::IncomingData.pm" is to use main::TD::Log::entry more simply. Is there
    some way, from the main program, I can pass a reference to the instance of
    TD:Log the main program uses to TD::IncomingData so I can reference the
    routines directly in TD::IncomingData? Something like:

    use TD::Log;
    use TD::IncomingData;

    incomingdatainit({some reference to TD::Log});

    And then, from then on, I can reference the routines in main::TD::Log as I
    would "normally" reference routines in the IncomingData module? (Like with
    "entry()" instead of "main::TD::Log::entry()"?)

    AND (2nd part of question), once I've done that, is there a way to tell if I
    have been passed main::Log and using that, instead of the routine not being
    accessible?

    Thanks!

    Hal
     
    Hal Vaughan, Aug 23, 2005
    #1
    1. Advertising

  2. Hal Vaughan

    John Bokma Guest

    Hal Vaughan <> wrote:

    > I have a module, TD::Log.pm (TD is the company name, so most modules
    > are in that directory). When I write a program, I use:
    >
    > use TD::Log;
    >
    > as expected. I also have other modules I use. I know a module can
    > reference the function "entry()" in the instance TD::Log in the main
    > program by using:
    >
    > main::TD::Log::entry("status: adding today's data to file");
    >
    > However, what I'd like to do, when I open a new module from main, say
    > "TD::IncomingData.pm" is to use main::TD::Log::entry more simply. Is
    > there some way, from the main program, I can pass a reference to the
    > instance of TD:Log the main program uses to TD::IncomingData so I can
    > reference the routines directly in TD::IncomingData? Something like:
    >
    > use TD::Log;
    > use TD::IncomingData;
    >
    > incomingdatainit({some reference to TD::Log});


    my $incoming = new TD::IncomingData;

    $incoming->init();

    :

    > And then, from then on, I can reference the routines in main::TD::Log
    > as I would "normally" reference routines in the IncomingData module?
    > (Like with "entry()" instead of "main::TD::Log::entry()"?)


    Read on Perl OO. There is an excellent book on it, or perldoc perltoot
    (for example)


    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Aug 23, 2005
    #2
    1. Advertising

  3. Hal Vaughan

    Anno Siegel Guest

    Hal Vaughan <> wrote in comp.lang.perl.misc:
    > I have a module, TD::Log.pm (TD is the company name, so most modules are in
    > that directory). When I write a program, I use:
    >
    > use TD::Log;
    >
    > as expected. I also have other modules I use. I know a module can
    > reference the function "entry()" in the instance TD::Log in the main
    > program by using:
    >
    > main::TD::Log::entry("status: adding today's data to file");


    The standard way of accessing entry() in package TD::log is

    TD::log::entry( ...);

    I wasn't even aware you can access it as

    main::TD::log::entry( ...);

    and, for that matter, also as

    main::main::main::TD::log::entry( ...);

    apparently with as many "main::"s as you want. This is a quirk in Perl
    but not something to be used in practical programs.

    > However, what I'd like to do, when I open a new module from main, say
    > "TD::IncomingData.pm" is to use main::TD::Log::entry more simply. Is there
    > some way, from the main program, I can pass a reference to the instance of
    > TD:Log the main program uses to TD::IncomingData so I can reference the
    > routines directly in TD::IncomingData? Something like:
    >
    > use TD::Log;
    > use TD::IncomingData;
    >
    > incomingdatainit({some reference to TD::Log});


    See perldoc Exporter.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Aug 24, 2005
    #3
    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. sam pal
    Replies:
    3
    Views:
    591
    E. Robert Tisdale
    Jul 16, 2003
  2. Maric Michaud
    Replies:
    0
    Views:
    7,219
    Maric Michaud
    Jun 24, 2006
  3. stefven blonqhern
    Replies:
    3
    Views:
    416
    stefven blonqhern
    Jul 5, 2007
  4. Replies:
    1
    Views:
    514
    Mark Rae [MVP]
    Sep 20, 2007
  5. Replies:
    2
    Views:
    165
Loading...

Share This Page