The practical value is the interface which defines how you log things.

If you use a global variable, then everywhere you want to log something you have to use that particular global variable. You've just tightly coupled all that code. If you decide to change how things work, you have to change a lot of code. With a method call, you don't have to know ahead of time what's you're going to get or how it's implemented. All of that stuff is hidden by the method call. Once you separate yourself from the implementation, you can change it without having to go through the rest of your code to maintain the tight coupling.

You still want to pass around references because you want every thing working with the same copy of things, and any changes apply to every thing that uses it. Since you want a reference, it's not too much of a leap to use one that's been blessed.

You don't need a slow or repititous call either. You make your own package that represents the logger. Behind that is all the gory details. When you decide to switch from L4p to something else, your code doesn't need to know that. The class name and the name of the logger aren't global variables: they are symbolic constants. They represent things you hard-coded (or determined) somewhere else so you don't have to know what it is ahead of time. Behind that package you figure out how to implement it, and are free to change the decision without rippling changes through the code base.

my $log = Log->new( SYMBOLIC_CONSTANT );

The Singleton is a way to control access without giving people direct access. When I use them, the actual object is a lexical variable in its package. Anyone who wants a reference to it has to go through the interface. They can't futz with package names. They get their reference to it, store it in whichever variable they like, and get on with life. You don't get that control with package variables.

--
brian d foy <bdfoy@cpan.org>

In reply to Re^2: Log::Log4perl and singleton design issue by brian_d_foy
in thread Log::Log4perl and singleton design issue by BUU

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.