At 18:31 2001-11-03 -0600, Mark Wilkinson wrote:
Someone please... save me before I choke!  Please say it isn't so!!
Tonight I really started to build a serious heavy-duty Win32::GUI
application, and I immediately hit a wall:

Is it really not possible to (usefully) program in Win32::GUI while under
"use strict"?!?  I say this because there appears to be **nothing** passed

This isn't so.

But it's unfortunate that none of the provided sample scripts use strict and warnings.


to the event-handling subroutines - not even a reference to the widget
itself!  This, IMHO, is absolutely insane!

No, but it's very impractical. I felt the same thing when I started using Win32::GUI.


That means that all of my widget refs, and every variable that is required in the event-handling subroutines, have to be global variables...

Obviously you haven't reached the "Singleton" chapter in your patterns book :)


which would make comp-sci professors roll over in their future graves... a "good" program should never need to have a *single* global variable, let alone *every* variable being global. If even the widget ref was sent to the event-handler I could attach the subroutine's required variables to its hash, but as things are it seems to break every rule I know about "good" programming style.


What you need is an entry point to your window variables. From there you can access all the contained controls.

1. You can do this with e.g. a singleton class for each of your windows. In each event handler you instansiate a new (i.e. the same) object of your class wich contains a property with your window object.

Examples: The GUI Loft and Perl Oasis:
http://www.bahnhof.se/~johanl/perl/


2. You can also do this with globals. Yes, it's poor form. In general it's a bad idea. But you should understand _why_ it's bad before you shun them. Globals are bad because (among other things) they introduce side effects at the largest possible scope. They increase what you need to know about a piece of code in order to understand it. Bad, bad, bad.

The way Win32::GUI works, they're very useful. After all, the nice, cool, com-sci approved singleton is really just an entry point to a single object -- a global object (with a twist, admittedly). And the way the globals are used makes it possible to think about this like we were back in Object Oriented Land again * relief *

But polluting the global name space with loads of vars is not necessary if you use a single list or hash for storing the window objects. That way you only have one global variable to keep track of.

Example: This is how you manage your windows built with The GUI Loft. An event handler might look like this:


sub ::btnFetch_Click { defined(my $win = $Win32::GUI::Loft::window{winFetch}) or return(1);

        my $url = $win->tfURL->Text();

        #Do something with the $url

        return(1);
        }


I'm not saying that it wouln't be _better_ having the window object passed to the event handler (it would), I'm saying it doesn't have to be all that bad using a single global to accomplish almost the same thing.


/J

------ ---- --- -- -- -- -  -   -    -        -
Johan Lindström                    Boss Casinos
Sourcerer                     [EMAIL PROTECTED]
                 http://www.bahnhof.se/~johanl/
If the only tool you have is a hammer,
everything tends to look
like a nail


Reply via email to