The problem is that an object, by definition, encapsulates its data. And yet you wish to externalize some portion of the object's encapsulated data so that it's available to any subs in the package, inaddition to object methods. That's messy. Furthermore, an object is usually "an" instance of a class, not "the" instance of a class. That means you can have several object instances of a class, each with its own separate object data. If there is to be a piece of class data that is the same for all object instances, your use of the global variable is the cleanest solution I can think of, so why jump through hoops to figure out a way to eliminate it?

An object's data isn't intended to be available before the object is created, and yet still unique to the object. You may be able to come up with some contortion using an inside-out object where objects are clones of some parent datastructure, but that's no better than what you've got now, and definately harder to maintain.

I guess my question is why is it so important that you eliminate a package global when it's doing what you want? And would it suffice to create a package-scoped lexical? By creating an outter lexical block that is as broad as the package, and declaring a lexical (my) variable within it, you'll have a variable that looks global to the rest of the package (except there's no entry in the symbol table), but is inaccessible from outside the package (because there's no entry in the symbol table -- assuming that's your goal).


Dave


In reply to Re: Getting an "object's" value before it becomes an object by davido
in thread Using an object's value instead of a global variable by jacques

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.