Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Fun with Farcical Factories (Was: Re^4: Your favorite objects NOT of the hashref phylum)

by BrowserUk (Patriarch)
on Mar 28, 2006 at 14:37 UTC ( [id://539708]=note: print w/replies, xml ) Need Help??


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

Although you've responded to your own post rather than me, I'll assume the guilt for 'now being dogmatic' :) I'll also accept the judgement that I'm "simply lacking in mental horsepower" to see how this works?

Following on from your logger example. Each place in your application you have something like

package Some::Class; use Logger; sub new { ... $self->{logger} = Logger->new; ... } sub print_to_log { my $self = shift; $self->{logger}->( @_ ) } sub someMethod { my( $self ) = @_; ... $self->print_to_log( 'some stuff' ); ... } sub someOtherMethod { my( $self ) = @_; ... $self->print_to_log( 'some other stuff' ); ... }

Could you explain to me how you would arrange for the logging from someMethod to go to one file, and the logging from someOtherMethod to go to a different file without changing Some::Class?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re: Fun with Farcical Factories (Was: Re^4: Your favorite objects NOT of the hashref phylum)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Fun with Farcical Factories (Was: Re^4: Your favorite objects NOT of the hashref phylum)
by aufflick (Deacon) on Mar 30, 2006 at 12:15 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://539708]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-19 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found