Re: OO Perl and Tk
by stvn (Monsignor) on Sep 09, 2004 at 19:20 UTC
|
... but I wonder if anyone has seen a good book that covers OOPerl and Tk, or if they know of a tutorial, or even some well written OOPerl/Tk code that I could peruse.
Well, I don't know of a good book on that specific topic, however, Tk itself is pretty object oriented, so you may want to start there.
Quick Disclaimer: I dont know what you app does, so I am just gonna throw out some things I have done in the past in similar situations.
You can start by taking any custom behaviors you have implemented proceduraly and making them subclassed widgets. Even if you are only adding a single method, or pre-setting some configuration parameters in the widget constructor, it can still be worth it to subclass because of the possiblities for extensibility it gives you later on.
Probably another good place to start is to look at the Tk "MegaWidgets" out on CPAN. You may be able to combine groups of widgets into a single mega-widget if they get reused in other places.
You may also want to think of the different layers of your application as well. n-tier design ideas need not only apply in multi-tier settings. Break out any non-UI parts into their own objects. This can have added benefit later on when you may want to port it out of Tk and into some other platform.
Another thing that I have found very valuable when doing this type of work is to build some kind of regression test. Having some way you can be sure your changes did not break anything will save you a lot of time. Then you can refactor and make these changes incrementally and test after each one.
As for the global variable thing, you will likely be able to get rid of most of them in the refactoring process, for the rest of them, all I can say is Singletons.
Hopefully some of this will be useful to you :)
| [reply] |
|
|
stvn
Thanks for the comments. Yes, it also struck me that as Tk is heavily object oriented that what I need to do is wrap each block of UI with my own additional methods so that I reduce the number of globals to basically the handles for the objects. Presently I have a whole lot of other data that gets globalised because I didn't wrap things up properly at the start.
Now all I need to do is go away and understand Singletons!
Thanks!
jdtoronto
| [reply] |
Re: OO Perl and Tk
by ketema (Scribe) on Sep 09, 2004 at 19:35 UTC
|
Oreilly is the way to go here:
Matering Perl/Tk
I love all their books. My only complaint though, is I hate building GUI's manually! Its way too much code, and takes forever. I'd like to know if you have a Visual GUI designer that you use for Perl/Tk. I have tried Activestate's Komodo 3.0, but I don't like it. Since I program mostly in windows(I know, Iknow) I have actually taken to .Net. I got the Perl Dev kit and visual Perl from ActiveState and using C#,Visual Studio, and a book called Programming PERL in the .Net Environment by Yevgeny Menaker & company I have been having a blast.
Since everything is an object in .Net I simply draw a GUI ina C# project and make it inheritable. Then using the specialized PerlNet syntax I create a Perl object and code all of the methods,properties and event handlers in PERL! Its great. Of course this does not make for crossplatform code, but it does work, and works quite well for what I need. | [reply] |
|
|
ketema,
Like you I hate building GUI's by hand. But I do so little of it, relatively speaking, that I can handle it. I am using Komodo 3.0, but whilst the editor portion is great, I am not sure I like the GUI builder either - but then I haven't tried it seriously.
As for O'Reilly - if I had all the money I spent with O'Reilly over the years I would be in for a new car! Sadly I must be portable, although nobody buys the Linux version of our product sales tell me it is a great selling feature with so many folk "thinking of going to Linux" these days.
Thanks for the suggestions!
jdtoronto
| [reply] |
|
|
| [reply] |
|
|
I just read your comment about using NET and Perl.
Pretty intersting idea - Especially under win32.
Recently I wrote a perl/Tk application > 2000 lines - actually it was my choice to use perl for the app and (i love perl quite a lot...) it was a hassle :(. The widget in win32 are pretty buggy and I never figured out, why some things never worked out (spreadsheet widget and events :() -
Can PERL.NET be an alternative in win32 enviroment?
Well you have to rely on activestate support? What do you
had to buy?
Hartwig
| [reply] |
|
|
I'd like to know if you have a Visual GUI designer that you use for Perl/Tk.
I have been working on such a beast for a few months now and am almost ready to realease a beta version that will act as a release candidate for v1.0. An older beta version of the program, called ZooZ, can be found
in my CPAN directory.
Note that the CPAN version is very old. Many missing features have been implemented and many bugs have been ironed out. I just need to do a little more testing before releasing the beta version. Once I do this, I will announce it on comp.lang.perl.tk. Any comments and bug reports are very welcome.
| [reply] |
Re: OO Perl and Tk
by eserte (Deacon) on Sep 09, 2004 at 20:37 UTC
|
I don't think 1200 lines of code is that much --- I have a Perl/Tk application with 17000 lines and a gazillion of global variables, but I'm still comfortable with it.
You don't necessarily have to switch to OO programming. Maybe you can first organize your code better, but still use the procedural style? As for the global variables: the only global variable representing a widget you really need is $mw. All other widgets may be expressed as subwidgets. See the Tk::mega manual about Subwidget and Advertise. If you have non-GUI data, then you may consider to move just these parts into modules. Consider to create megawidgets if possible (the Tk::mega manual also helps here). Limit the scopes of variables as much as possible by using functions or just blocks, e.g.:
# ... code before
{
my $f = $top->Frame->pack; # frame needed only for layouting pur
+poses
$f->Label(...)->pack(...);
$f->Label(...)->pack(...);
...
}
# ... not interested in $f anymore ...
| [reply] [d/l] |
Re: OO Perl and Tk
by zentara (Cardinal) on Sep 09, 2004 at 19:51 UTC
|
I've been thinking along the same lines. I've been working on a complex procedural Tk game, and it's finally dawning on me how much cleaner it would be if each player was an object.
I don't know "how well written" it is, since I'm a procedural programmer myself, but I did make a TripleRotatingWheel demo, which made it into the Tk::Zinc "user-contributed-examples". It does demonstrate how to make a "Wheel Class" and use it. I's about as simple as you can get. You might want to check out the 'zinc-demos' too, it has quite a bit of nice code examples.
TripleRotatingWheel
I'm not really a human, but I play one on earth.
flash japh
| [reply] |
|
|
zentara:
I have downloaded your code and will paw over it tonight when I have some time. It looks neat, and although I have looked at Tk::Zinc before I have never had a chance to use it.
My biggest enemy is time. I wish I could just spend the time necessary to learn all these new techniques - but nobody wants to pay for that!
jdtoronto
| [reply] |
|
|
Time, yes time. Although I don't see Time as an enemy, I can understand your situation. I have plenty of time, but then again, all I own is a computer, a hotplate, and my bicycle. Life is a bunch of trade-offs, isn't it? I spend my time hacking at Perl, to make the days "pass faster", too much time gets to be boring. Thank God for Perl. :-)
I'm not really a human, but I play one on earth.
flash japh
| [reply] |
Re: OO Perl and Tk
by ni-s (Initiate) on Sep 09, 2004 at 19:35 UTC
|
O'Reilly have "Mastering perl/Tk" which covers Tk pretty well.
My favourite OO book is Damian Conway's "Object Oriented Perl" (Manning), but it doesn't really talk about Tk.
It may be worth looking at the composite widgets in Tk distribution if that is what you are trying to do. | [reply] |
Re: OO Perl and Tk
by Courage (Parson) on Sep 10, 2004 at 10:11 UTC
|
In order to make GUI program more maintainable, I would advise using approach of separaing GUI design and logic.
I mean approach described in
http://www.vkonovalov.ru/vtcl-usage/Using_vtcl_for_creating_Tcl-Tk_GUI_for_Perl.html
It is not perlTk, but, in addition to pure Tcl, there is a possibility to use perlTk syntax, so most of code will work unchanged.
I am not sure whether you're going to redesign your GUI in VTCL, but you could try using this approach for new applications. | [reply] |
Re: OO Perl and Tk
by OhReally (Monk) on Sep 10, 2004 at 08:33 UTC
|
You dont mention what you are mainly doing in Tk but I found Tk::Cloth very useful as a better way to manipulate and group objects together with data on a canvas in Tk. It might be worth a look. | [reply] |
Re: OO Perl and Tk
by Washizu (Scribe) on Sep 10, 2004 at 13:00 UTC
|
I wrote a Tetris clone called Spew using OOPerl and Tk. The source code is included along with a windows binary.
Download Spew here
The main problem I've come across with OOPerl and Tk is that you still kind of have to treat the MainWindow object as a global variable. I get around that by passing in the MainWindow object as a variable to the objects that need it.
| [reply] |
|
|
| [reply] |
|
|
Of course, you could only be sure by inspecting every line of code in there.
There is only one line where I make a local reference.
use lib 'C:\Documents and Settings\Ben Garvey\My Documents\spintris\li
+b';
That was so perl2exe could compile the thing. For some reason it can't compile it if you use a relative path even though it works fine through the script. To get start.pl to run properly you'd have to change it to this for your system.
#use lib 'C:\Documents and Settings\Ben Garvey\My Documents\spintris\l
+ib';
use lib 'lib';
The executable is for people who want to try the game out but don't have perl and all the libraries installed.
There are no self-unpacking files. There is a zip file that puts files into directories when installed, but only sub directories inside the one you unzip to.
Lastly, the program is for download on a website that has far too much personal information about me to allow me to get away with anything nefarious.
Feel free to call me on any evil code. It's all there to see.
| [reply] [d/l] [select] |
|
|
|
|
|
|
Sorry to hijack this thread yet again, but here are some
screenshots of the game.
| [reply] |