Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I'm still new to object oriented perl and am having a very difficult time with one thing.

I have a main script and then I have a number of packages. Each package has a class and some methods.

At least two of these classes will be used by many (if not all) of the other packages. These two are called:

Config.pm
Creates an object containing all system config settings

Login.pm
Creates an object containing all the session/login info about the currently logged-in user.

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.

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.

Foo.pm
Bar.pm
Test.pm
Grok.pm


I'm not sure how to do this?

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.

Is my only solution to do one of the following or is there something more elegant?

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->{_login}->{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 . . .?

or...

2) Something to do with prototyping?

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!).

In reply to Creating 'universal' instances of objects? by Seumas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (10)
    As of 2014-08-21 07:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (128 votes), past polls