I would create the new singleton instance when you need it, rather than in the initialiser, but I'll use your example where you create the object once in the initialiser.

In your example $self->{logger}->( @_ ) I will assume you meant something like $self->{logger}->log( @_ )

package Logger; my $singleton_object; sub new { my $class = ref $_[0] || $_[0]; $singleton_object ||= bless $singleton_object, $class; } sub logfile_handle_for_method { my $self = shift; my ($package, $subroutine) = @_; # not cross-platform... $package =~ s!::!/!g; my $filename = "/var/log/$package/$subroutine.log"; return $self->{filehandles}{$filename} if $self->{filehandles}{$fi +lename}; my $fh; open $fh, ">$filename" | die; $self->{filehandles}{$filename} = $fh; } sub log { my $self = shift; my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask) += caller(1); my $fh = $self->logfile_handle_for_method($package, $subroutine); print $fh, @_; }
Now you could correctly point out that the same could be achieved with a file scoped hash and a package sub (rather than using OO). But if you had just used a global filehandle with a print statement at every log point (as I often see in code) you would not be able to extend it.

And that's really one reason why drilling rules like "use Singletons not globals" into younger or less skilled developers - it may not often buy them much, but it might prevent them from doing something stupid. You would hope that you could teach them about how to decide when to apply abstraction (which is all this really is about) but experience tells me that an abstract concept like abstraction is not universally understood by people wanting to be programmers.


In reply to Re^2: Fun with Farcical Factories (Was: Re^4: Your favorite objects NOT of the hashref phylum) by aufflick
in thread Your favorite objects NOT of the hashref phylum by blogical

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.