Passing a Module as an Object or Reference

H

Hal Vaughan

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
 
J

John Bokma

Hal Vaughan said:
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)
 
A

Anno Siegel

Hal Vaughan said:
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
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top