G'day Tobias,

Problems with what you've posted:

Having said that, declaring $debugger as being scoped to the entire package leaps out at me as being a potential source of problems (either now or in the future). Consider this scenario:

$ perl -Mstrict -Mwarnings -le ' package myDebugger; my $debugger; sub GetInstance { defined $debugger ? $debugger : DebuggerInit() } sub DebuggerInit { $debugger = "singleton" } # a typo later in your code (perhaps "$debug = 0;" was intended) my $debug = 1; # ... $debugger = 0; package main; print myDebugger::GetInstance(); print myDebugger::GetInstance(); ' 0 0

Now see what happens if the scope is changed such that only GetInstance() and DebuggerInit() can see $debugger:

$ perl -Mstrict -Mwarnings -le ' package myDebugger; { my $debugger; sub GetInstance { defined $debugger ? $debugger : DebuggerInit +() } sub DebuggerInit { $debugger = "singleton" } } # a typo later in your code (perhaps "$debug = 0;" was intended) my $debug = 1; # ... $debugger = 0; package main; print myDebugger::GetInstance(); print myDebugger::GetInstance(); ' Global symbol "$debugger" requires explicit package name at -e line 11 +. Execution of -e aborted due to compilation errors.

Fixing the typo (at line 11):

$ perl -Mstrict -Mwarnings -le ' package myDebugger; { my $debugger; sub GetInstance { defined $debugger ? $debugger : DebuggerInit +() } sub DebuggerInit { $debugger = "singleton" } } # a typo later in your code (perhaps "$debug = 0;" was intended) my $debug = 1; # ... $debug = 0; package main; print myDebugger::GetInstance(); print myDebugger::GetInstance(); ' singleton singleton

Please create a minimal, self-contained script that reproduces your problem as described in the "How do I post a question effectively?" guidelines. As ++rjt has pointed out, by doing this you may resolve the problem on your own. If you need further help, please also include the missing information I alluded to above and also described in the guidelines I've linked to.

-- Ken


In reply to Re: Singleton and unblessed refereces. by kcott
in thread Singleton and unblessed refereces. by tobias_hofer

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.