Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Singleton and unblessed refereces.

by kcott (Archbishop)
on Jul 09, 2013 at 07:29 UTC ( [id://1043255]=note: print w/replies, xml ) Need Help??


in reply to Singleton and unblessed refereces.

G'day Tobias,

Problems with what you've posted:

  • You haven't shown what DebuggerInit() does.
  • You haven't shown how you run this code.
  • There's no self-contained script that we can run to reproduce the problem.
  • You don't show actual or expected output nor any error (or other) messages.

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

Replies are listed 'Best First'.
Re^2: Singleton and unblessed refereces.
by tobias_hofer (Friar) on Jul 10, 2013 at 07:34 UTC
    Hi,

    Sorry for my late replay.
    I still have not found the root-cause.
    I made an example where i replaced the OLE-interfaced debugger by the OLE-interface of the excel application (because I do not suppose someone has a PLS UDE debugger at home) and it is working fine - its completely bizzare to me. However, if i am doing the same with the debugger OLE Application i get the $debugger variable as undefined.

    Please have a look

    Here my example code:

    TestMe.pl
    ########################################## #Perl file which shall use the "debugger" ########################################## use strict; use warnings; use diagnostics; use myDebugger; my $_db = GetInstance(); print "Does not work\n" if (not defined $_db); print "Is working fine\n";
    myDebugger.pm
    ########################################## #Perl file myDebugger.pm ########################################## package myDebugger; use strict; use warnings; use Win32::OLE; use Win32::OLE::Const "Microsoft Excel"; use Win32::OLE qw(in with); use Win32::OLE::Variant; use vars qw($VERSION $HEADER @ISA @EXPORT); use Exporter; $VERSION = q$Revision: 1.1 $; $HEADER = q$Header: Some CMS data... $; @ISA = qw(Exporter); @EXPORT = qw( GetInstance ); # export subs my $debugger = undef; sub GetInstance { if ( defined $debugger ) { return $debugger; } else { DebuggerInit(); defined $debugger ? return $debugger : print " Init error!"; return 0; } } sub DebuggerInit { # Instead of an Debugger, I use here the Excel-Application. # Both excel and the debugger application are interfaced by OLE # Thus I think its a good replacement as an example. # Also the way how the OLE Application is the same # as the originally used UDE PLS Debugger. $debugger = Win32::OLE->GetActiveObject('Excel.Application'); unless ($debugger) { $debugger = new Win32::OLE( 'Excel.Application', \&main::QuitA +pp ); } } sub main::QuitApp { exit(); } 1;

      I would add far more checks/tracing to DebuggerInit. Maybe ->GetActiveObject() does differ in behaviour for the debugger, or creating the new object fails if the debugger application is not already running. Output the functions called and the results in DebuggerInit.

      Your choice of setting up a call to exit() in the object destructor:

      $debugger = new Win32::OLE( 'Excel.Application', \&main::QuitApp );

      ... strikes me as weird. I use the call to properly tear down the OLE object, and not to exit the Perl script in a very hard way. Maybe the debugger application does not like that method of improper cleanup. As a first start, I would not try to quit the OLE application at all upon script exit. This will likely leave you with headless processes accumulating, at least it does with Excel. After trying that, I would look at finding the proper way to quit the debugger application. Maybe you need to do nothing, or you need to do the same thing that the Win32::OLE documentation suggests, except in the appropriate way for your debugger:

      $ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;}) # calls $ex->Quit() when the Perl program ends or $ex goes out of +scope

      As a personal style option, I would not cache the $debugger in a file-global lexical variable. In most cases, using a proper global variable in the packages is better for me, because it allows me to explicitly undef such variables for debugging purposes.

Log In?
Username:
Password:

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

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

    No recent polls found