<?xml version="1.0" encoding="windows-1252"?>
<node id="280674" title="Creating 'universal' instances of objects?" created="2003-08-04 09:48:07" updated="2005-08-14 02:15:47">
<type id="115">
perlquestion</type>
<author id="5565">
Seumas</author>
<data>
<field name="doctext">
I'm still new to object oriented perl and am having a very difficult time with one thing.
&lt;br&gt;
&lt;br&gt;
I have a main script and then I have a number of packages. Each package has a class and some methods.
&lt;br&gt;
&lt;br&gt;
At least two of these classes will be used by many (if not all) of the other packages. These two are called:
&lt;br&gt;
&lt;br&gt;
&lt;strong&gt;Config.pm&lt;/strong&gt;&lt;br&gt;
Creates an object containing all system config settings
&lt;br&gt;
&lt;br&gt;
&lt;strong&gt;Login.pm&lt;/strong&gt;&lt;br&gt;
Creates an object containing all the session/login info about the currently logged-in user.
&lt;br&gt;
&lt;br&gt;
The problem is that specific instances of both objects need to be accessed by multiple (if not all) other packages. That is, I don't want to do a 'use Login; use Config;' inside of *every* package that needs the config or login data.
&lt;br&gt;
&lt;br&gt;
If I have the following packages, I want them ALL to access the same instance of the login or config object, instead of each having it's own copy/instance - including the main script itself.
&lt;br&gt;
&lt;br&gt;
Foo.pm&lt;br&gt;
Bar.pm&lt;br&gt;
Test.pm&lt;br&gt;
Grok.pm&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I'm not sure how to do this?
&lt;br&gt;
&lt;br&gt;
I've been looking through the Perl Cookbook as well as Merlyn's newer Object/References book and if they contain the answers to this, I didn't know it when I saw it.
&lt;br&gt;
&lt;br&gt;
Is my only solution to do one of the following or is there something more elegant?
&lt;br&gt;
&lt;br&gt;
1) Create an instance of the config and login objects in the main script. Then when I access any other packages or classes from the main script, have the first arguement (after the class/$self) be a reference to $Main::login so that each package could then be accessing the main instance of it? Such as $main::login-&gt;{_login}-&gt;{session_id}. And if I access a class from another class that itself is accessed from the main scripted (say, main.pl accesses Foo.pm and Foo.pm accesses Bar.pm), I would have Foo.pm pass to Bar.pm a reference to the reference of $Main::login or $Main::config . . .?
&lt;br&gt;
&lt;br&gt;
or...
&lt;br&gt;
&lt;br&gt;
2) Something to do with prototyping?
&lt;br&gt;
&lt;br&gt;
Needless to say, I'm a bit lost right now. I've spent a few days trying to figure this one out and my little project is at an impasse until I do something (since, obviously, config and login objects are a bit essential!).
</field>
</data>
</node>
