Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Passing a Module as an Object or Reference

Reply
Thread Tools

Passing a Module as an Object or Reference

 
 
Hal Vaughan
Guest
Posts: n/a
 
      08-23-2005
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


 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      08-23-2005
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

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      08-24-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Object reference not set to an instance of an object" Weird thing happens with reference a link nguyentrongkha@gmail.com ASP .Net 1 09-20-2007 09:46 PM
Passing a reference as a reference pamelamishra@gmail.com Perl Misc 2 08-16-2007 09:48 PM
Passing a reference to an object from inside the object's member functions? stefven blonqhern C++ 3 07-05-2007 06:49 PM
Re: module docstring, documentation,anything? please note is the module type/object NOT some module Maric Michaud Python 0 06-24-2006 12:42 PM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57