So I want a logger object, and only one logger object thought it may be used in various packages. I follow the simple instructions at Implementing the singleton pattern in Perl but when I test it I see I get two objects. In the following test one gets its 'Log' value altered and the other does not ...

The test ...

perl -MData::Dumper -Mlib=. -MHelper::Lgr -E' \ $l=Helper::Lgr->getInstance();$l->log(0,"Log1"); \ $z=Helper::Lgr->getInstance();$l->set('Log', 4); \ print Dumper $l; \ print Dumper $z' Get instance called from main Not yet instantiated Sun Dec 29 18:51:08 2019 Log1 Get instance called from main Not yet instantiated $VAR1 = bless( { 'LogName' => 'CommandLine.log', '_continuation' => 0, 'Screen' => 0, 'LogDir' => '/home/random/log', 'fh' => \*Helper::Lgr::__ANONIO__, 'Log' => 4 }, 'Helper::Lgr' ); $VAR1 = bless( { 'LogDir' => '/home/random/log', 'Screen' => 0, 'LogName' => 'CommandLine.log', '_continuation' => 0, 'Log' => 0, 'fh' => \*Helper::Lgr::__ANONIO__ }, 'Helper::Lgr' );

Here is the start of the Logger pm that I am using, the set method is pretty much what you expect,so not reprduced here ...

#!/usr/bin/perl # # Lgr.pm # # Controls both logging to a file and to the screen # you can set a verbosity for each independently # package Helper::Lgr; use strict; use warnings; use v5.10.0; use File::Basename; use File::Path qw(make_path); use FindBin; use Data::Dumper; my $script = basename( $0, '.pl', '.pm' ); # Logger is a classic singleton pattern. We only want one instance in +our code my $instance = undef; sub getInstance { say "Get instance called from ".( caller ); if ($instance) { # Already instantiated, check if we need to chang +e any settings say "Already instantiated"; return $instance; } else { say "Not yet instantiated"; } my $class = shift; my $instance = shift; # settings to use $instance = {} unless $instance; die "$instance is not a hash ref\n" unless ref $instance eq 'HASH' +; bless $instance, $class; # we bless early so we can call methods o +n ourself ... And much more ...
What simple fact am I missing that I get a fresh logger each time?

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to My promiscous singleton by Random_Walk

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.