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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |