Learning Perl/Tk
Greetings, all. This will be the first in a series of reviews dealing with some Tcl/Tk books I've recently acquired. Since I already have some Perl in my background, I took this one first, both to sharpen my Perl skills, and to find out what this Tk thing is all about. Given that my GUI experience is limited to Smalltalk and Java, Tk is quite easy to use. With Smalltalk, I was too busy wrapping myself around OO theory to enjoy the interface, and Java always seemed to make GUI stuff more difficult than it needed to be (although I still love it). Perl and Tk are strong partners, because they share a philosophy of getting things done without a lot of fuss. Perl and Tk are excellent replacements for any GUI scripting language you might use (read: VB). Read on to see how to jump in!
What's the book about?This is another book in O'Reilly's Learning series (of which Learning Perl really saved my butt in college), which is dedicated to teaching the fundamentals of a certain topic. I want to compare this series with the Learn XY in 21 days type of books, although I believe that would generally be an insult to the quality of O'Reilly. Once you finish this book, you will have enough of an understanding of Tk to be able to do most small projects. You will know most widgets (although I'll admit my own knowledge is limited here), and will generally be prepared to be productive with Tk in Perl.
What's Good?
In order for you to be able to evaluate the usefulness of this book, it will probably help to understand where I'm coming from. I have a BSCS from Georgia Tech, and have enough languages under my belt to do some damage (Lisp is cool!). In fact, I learned Perl originally for a networking project using Learning Perl. It gave me enough to do what was needed. Having said that, I don't live to program, and in fact I'm not big on reading language books. I don't know every language under the sun, and I don't necessarily learn them with the greatest of ease. In other words, my results should be duplicable by most programmers. The most important thing when reading this book is to know Perl (at least have written some stuff in it), and probably have an idea of what to expect when writing GUI code.
Going into this, my Perl was definitely rusty (having not touched it in a while). I didn't have any trouble diving straight in, however. The Perl constructs used are not overly complicated, and my knowledge was sufficient. (NOTE: make sure you have a very recent version of Perl installed. My Redhat 5.2 needed to be upgraded to m4 before the examples would work. Also, get the errata from O'Reilly.) The early chapters deal with basic constructs and widgets, and spend a great deal of time on the geometry managers (go figure). Each chapter introduces a new widget, although some are used before they are introduced (just nod and smile when you see those and don't worry). There are plenty of examples, code fragments, and exercises to keep anyone busy. I tried to work as many as I could, to get a feel for the language, and generally felt like they were helpful. I never felt completely lost or confused, and generally followed things without much trouble. Having finished the book, I feel confident that, given a little work on my Perl, I could write a useful application with Tk, especially given some research on CPAN for various contributed modules. For me, the book worked.
What's Bad?Nothing in this book is particularly bad, although there are a few nits I'd like to pick. First, the early emphasis on geometry was somewhat interesting. I'm not sure why I care about grid vs pack when I can barely create a button to put on the screen. For that matter, frames are referenced in a short chapter late in the book, after being used all throughout. If the concept is so basic, why not put it toward the beginning? Also, there were times when the author mentions that an option is esoteric, or generally unused, and then spends much more time than necessary on that point. If it's so esoteric, why is it being covered in a basic book like this one? Finally, there were a few times that the book did not explain a point well enough to me, and I had to divine the answer down the road (like configuring scrollbars). It was not a major issue, but there were some things that could have been clearer.
What's In It For Us?If you want to learn Tk using Perl, this book will let you do that. It gives a solid introduction to the topic, and on completion, you will be a useful Perl/Tk programmer. Just know your Perl going in, and you will be fine.
Purchase the book over at Computer Literacy.
- Preface
- Introduction to Perl/Tk
- Geometry Management
- The Basic Button
- Checkbuttons and Radiobuttons
- Label and Entry Widgets
- Scrollbars
- The Listbox Widget
- The Text Widget
- The Canvas Widget
- The Scale Widget
- Menus
- Frames
- Toplevel Widgets
- Binding Events
- Composite Widgets
- Methods for Any Widget
- Configuring Widgets with configure and cget
- Operating System Differences
- Fonts
Index
Well, you can set a frame to use the grid geometry manager, so you could at least simulate a grid by using labels, entrys or whatever and using grid() to arrange them. I don't use the VB grid control so I'm not sure exactly how you'd implement it, but if you know enough about both languages I suspect you can make something that will do what you want.
Just Another Anonymous Perl/Tk Hacker
Perl/GTK+ will be the way to go.. the perl TK module is kinda bloated .. people just need to keep in touch with the author of perl/GTK+ so we can see releases more often than every 4 months. (CVS maybe?)
I grow weary with folks making the claim that Perl is "ugly" or "unstructured" without being specific. Most often they are refering to regular expresions which are ugly but, extremely powerful.
If you want clean looking Perl code don't use regular expressions.
I'd love to do more work in Perl/GTk+, but the docs suck dead gophers through garden hose.
:)
Perl/Tk may be bloated, but it works, is much easier to understand, and is much better documented.
Functionality and ease-of-use beat out bloat anyday - why else use Perl?
(Although I wish the Perl compiler would handle use and XS, so I could put binary versions of the utilities I write on boxes that don't have Perl on them)
DG
Why is anyone reviewing paper books?
I don't care that much about trees, but all you youthful slashdot readers just don't realize how heavy, and how quickly outdated most of this stuff becomes.
x
Aldo Calpini has got his 100% Perl answer to Perl/TK for the W-word working quite nicely. You can take part in the development of Win32:GUI by subscribing to the mailing list...send mail to
majordomo@httptech.com with
subscribe perl-win32-gui you@yourdomain.com
(your email) in the body. CPAN isn't quite current; the mailinglist is the place for the latest and greatest.
An entire spreadsheet package has been built over it, using STk (Scheme/Tk)
http://www.esinsa.unice.fr/xxl.html
I wish O'Reilly would release their books as PDF's. I'd pay the same price, and they'd be way more useful to me.
In the three weeks since I bought Learning Perl/Tk it has paid for itself at least twice over. That pretty much negates the "outdated" aspect of buying a book -- it served the particular purpose I bought it for at the time I bought it, and put money in my pocket besides. I just wish I could claim the same about Discworld novels.
PDF or HTTP files might fill the same need, but there are two important differences. First, I find it much, much easier to leaf through a book and scan the pages for information I need than I do jumping through a stack of windows on a screen. And second, I have no laptop so PDF files do me no good when I'm on the bus (I use the enforced downtime to do offline study). Other people may feel differently and their mileage may vary.
Just Another Anonymous Coward
everything with a *.pm is a perl module per say...
Tk is a Graphical User interface to the perl language. I got the book perl/Tk and it is really awesome!
INSTALL
-------
I assume you don't have some weird directory structure and you are using perl5.0005_2 aren't you? (I tried the sub ver 4_.. like they said would work in UNIX.. didn't work for beans..)
Once you install perl:
perl MakeFile.PL
make
make test
make install
then install Tk in the tk directory with the same commands..
make sure your reference to perl is in the right
place (the demos are not set obviously to yours)
BOOK
----
I understand the gripe about why compare the geometry and pack. - The point is that by manipuating pack you can change the way the giu buttons and stuff are displayed. It is a very important concept in Tk.
I have found "Learning Perl" by Tom Christiansen and Randal Schwartz (O'Reilly) to be a fairly good introduction.
gtk.org has 0.500
The philosophy of Perl is to let the programmer decide on matters of structure, syntax, etc. The aesthetic of obfuscation with Perl is one of those things that gets a lot of attention and is rather interesting in a quirky sort of way.
But Perl can be structured to look a lot like C, if you want that. For that matter, you can write obtuse C code if you really want to.
I guess what baffles me is how this point gets lost on so many folks. A good craftsman doesn't blame the tool for his mistakes.
After beating my head against a Perl-y wall for a couple of weeks, I turned to TCL. That was so natural for me that I was doing interesting stuff within hours.
After a week or so, I found the TclXML package and started playing with it. Regexp suddenly became comprehendable on the second reading. I was soon sending bug fixes back to TclXML's author.
Finding TCL has been one of the biggest power trips I've had in a loooong time. It's so cool to write a script (with GUI) under MacOS, and it looks like a Mac app. Reboot into Linux, run the same script, and it looks like an X11 app. (I suppose it would be the same under Winblows... assuming I cared.)
So maybe I'm strange too. But wow! it feels good!
-- Dirt Road
I don't like Perl. Perl is hacker's language - too many quick hacks on the net written in Perl ended up being final solutions.
I don't like TK. It offers nothing that other languages can't offer.
Thank you.
Tcl doesn't try to be the world's most powerful language, and it isn't in the same space as Perl or Python. For what Tcl is, it's actually not bad.
Perl, tcl are the Visual Basic of the Unix world.
Ubiquitous but lame.
Yet another book that makes me yawn.
>after both of us trying to find out even the most basic information about Perl
Sounds like you were trying to look something up. Use Programming Perl or Perl in a Nutshell as a reference manual.
If you already know how to code in C, you probably don't need Learning Perl. Pick up the others instead.
VA_BOFH
If were a small simple program maybe, but if I were to convert some apps I had about, it would be pretty ugly stuff. I had found it more profitable to use the vb experience to reengineer the process and come up with better Python code. This is an interesting idea tho...
Joe Robertson
jmrobert@ro.com
Well this was IMHO one of the better reviews to be posted on /. It was more than just a summary like so many others.
If only TCL wasn't so strange. (At least to me and I've been using it since 1994!) You wouldn't need to graft Tk onto Perl. On the other hand I find Perl's syntax to be not strange but down right poor.
Interestingly enough when I was at the Tcl BOF at the 1998 USENIX conf Outserhout seemed surprised that more people were interested in Tk than Tcl.
Ron Rangel
. . . but then again, that's the stuff that religious flame wars are made of. I find Perl's syntax pretty natural once you learn what all those weird regular expression codes mean. I've gotten into discussions (no fistfights yet, thankfully) with hardcore C programmers who don't like Perl because it's so unstructured. That is, in fact, the exact thing I like about it. There's enough structure that your code tends not to become spaghettified but not enough to get in the way of coding. For me at least, it lets me think about "How am I going to solve this problem?" rather than "How am I going to force to help me solve this problem?" Programs go together quickly and easily, and tend to be more readable than your average C program. (And having said that, Perl helped me learn C. Go figure.)
In short, it lets me write code the way I think. If it doesn't let you write code the way you think, well, that's fine. Let a thousand virtual flowers coded in a thousand different languages bloom.
-- Just Another Anonymous Coward
Dunno bout you, but I _like_ well-indexed paper books. Reading computer displays can be much more taxing than reading book print. Also, books tend to be hardier and have longer battery life than laptops..
Besides, I learn best when I have a book (usually ORA), a few browsers, a few rxvts viewing manpages, and a few vi windows. Makes for very rapid education..
Does Tk have a grid widget like in VB ?
Thanks, Aaron Newsome
Posted by FascDot Killed My Previous Use:
...at least the last time I tried to use them about 3 months ago.
A lot of the basic properties are there, but there's very little in the way of tutorials on higher level stuff.
Posted by FascDot Killed My Previous Use:
The only FTP site I could find had a version number of 0.1.17--that can't be right. Where's the latest?
I have this book, and it's nice to have a solid book, it's not THAT much more than what's out there already.
... which is fine for hacking trivial cgis in your bedroom, but almost useless for long-term ongoing team projects. languages like pl/sql and ada force you into nice structures and enforce data types.
i *very* much doubt perl/tk is faster than VB5, and it doesn't even come close to VB's database app RAD capability.
I couldn't disagree more with your evaluation of Learning Perl. It had me writing working (albeit goofy) perl scripts before I even got out of the first chapter.
All in all, I would say that it was the best programming language book that I have ever read.
Of course, it goes without saying that everyone is different. That's why I would recommend going to a bookstore and checking things out for yourself.
Does anyone know of any programs to convert Visual Basic programs to Perl/Tk programs automatically, or at least semi-automatically? Something like that would be very handy to remove one of the major arguments small shops have against moving away from MS OSs. If their in-house VB programs can be automatically moved to Perl/Tk and run faster to boot, moving to Linux or FreeBSD would be a much easier sell.
Doug Loss
I don't disagree with anything you've said. What I'm talking about is something to take existing VB programs generated by people who used VB to get some need-specific job done (and who probably couldn't have programmed in any other language anyway) and convert it to Perl/Tk (or Python, or anything other than VB). Would it generate ugly code? Almost undoubtedly. However, it would have moved all their proprietary stuff to a non-Windows OS. It could then be cleaned up as needed. The upshot is that if they have to rewrite their VB apps in a better language, they won't do it and they won't move to Linux. As I said earlier, that's the point of the convertor, to get them to move.
Doug Loss
I know about this. It's a Java applet, IIRC, so it requires java to be installed on the machine. Also, being binary only it will run VB code, but it doesn't convert it to something native to the system it's running on. That would mean that any maintenance or modification to the VB app itself would still have to be done in VB, and the use of VB is what I'm trying to get away from.
Doug Loss
Well, it's definitely faster that VB5 on a Linux system. And without getting into a religious war about whether VB is better/worse than whatever other language we're talking about, my point isn't just to get better performance out of the VB apps (although I consider your opinion about VB5's performance unlikely) but to get rid of VB entirely as it just isn't a viable non-MS OS possibility.
Doug Loss
Geometry management is an extremely powerful and useful concept. I don't know Perl/Tk, but I do know Tcl/Tk, and I can tell you that without geometry management, Tk would be just another "me too!" GUI toolkit. Geometry management lets you construct user interfaces quickly without worrying about the details of where exactly all the widgets get placed on the screen.
It's garbage. A coworker of mine seems to agree - after both of us trying to find out even the most basic information about Perl the other day, we both independently wound up at bookstores that evening looking for something better. I came up with:
Perl by Example by Ellie Quigley
and
Perl Power! (can't remember author)
Both are quite nice - and you'll _really_ want Perl by Example - Perl is so obtuse at times that you'll need the line by line examples to figure somethings out. How 'Learning Perl' ever came to be respected, I'll never know - unless it's because the authors are influential, which is a ridiculous way to evaluate a book, IMO.
Anyway - my 2 cents' worth. Good luck!
I very much agree with the suggestion to go to :)
the store and check them out in person. Everyone
learns a little bit differently than the person
next to them, and that's the only way to be
sure...other than nuking the site from orbit,
of course!
I was just thinking about posting this to a newsgroup but this seems like a reasonably on-target forum; yesterday I tried out the Tk/Perl tax package recently posted on Freshmeat. It failed, saying Tk.pm wasn't installed. I do have both Tk and Perl packages installed but no file by that name. I searched the perl archives and didn't find a Tk.pm file, and there was nothing by that name in the Tk module directory. I'm sure the problem is my lack of familiarity with the Perl module system but I'd appreciate any advice. (This is going to run on MkLinux so pointers to source would be helpful.) Thanks....
What I'm listening to now on Pandora...
And since perl/TK works just fine on windows, you're not getting hurt at all, it's just all value-added. Oh, and you don't have to pay $1K+ (US) for the compiler.
Any program that translated VB to Perl would, by necessity, generate ugly code because VB code is just generally ugly. Better to do the job right the first time, I say. Or, you can always just wait for Micro$oft to come up with VB for Unix. >:)
There is a Grid widget, but it's a little flaky.
Check out the FAQ (I can't remember the URL, but
it's worth poking around the CPAN directory on
CPAN, it's in a module listing included there).
The Tk module's worth the masses of disk space it
uses up just for the TkPod addon.
Chris Wareham
I've found the tutorial to be almost as good
as a reference. For the more detailed points I
currently turn to the mailing lists, which have
some very knowledgable posters.
Yeah, the rest of the GTK+ docs are a little
sparse at the moment. For low level stuff, volume
one of the O'Reilly X Programming series is good,
the one on Xlib. The GDK in particular maps quite
closely onto the functionality of Xlib - but with
a much, much nicer API.
Chris Wareham
(see subject line)
Chris Wareham
Perl and Tcl may be lame if you're after speed and compactness, but for rapid solutions to everyday
problems they're fantastic. After all, that's
what VB is used for in the Windows world - quick
apps that may be a little kludgy and slow, but
that get the job done without taking an age to
program.
They're tools for a certain job (or jobs), don't
knock them.
Chris Wareham
Tcl has always struck me as a little less anarchic
than Perl. The KDE to Perl's Gnome if you're
into weird analogies.
With version 8, Tcl/Tk really came of age, and I
have actually found myself questioning my
otherwise firm devotion to Perl. Especially when
I encountered oodles of memory leaks in the Perl
Tk module.
Chris Wareham
Tell you what: when I have an extra cordless PDA that I can toss around, spill coffee on, use to annotate various pages of documentation, place any convenient thin object (including, temporarily, my fingers) in to mark my place, has sufficient resolution so lots of text is readable from a few feet away without strain, and can afford to misplace and buy another copy once in a while, lemme know.
obsolete, my arse...
I've finally had it: until slashdot gets article moderation, I am not coming back.
There is already a VBVM (VisualBasic Virtual Machine) for linux, but its in binary only. I personally lost the URL for it, but it wasn't hard to find.
> 99% of the worlds useful information is still in printed media. Printed media still have great advantages over online media: > lighter, smaller, and more portable; when was the last time you dragged your computer into the bathroom? - Im building a wearable computer > don't need electricity - Whats it matter when I already have my computer on? >much higher resolution; - 640x480 is fine. >use passive lighting; - thats what the brightness knob is for >easier on the eyes - brightness knob again. >more permanent urls tend to change a lot - save a local copy >I love the smell of new books ;} - get one of those air freshners for cars that smells like a new book, or just use the computer in Barnes&Noble :)
I have used Perl/Gtk and it is VERY easy, documents would be nice.. but the same information could easily be reached by reading the source to slash.pl, iceconf, or any other perl/gtk app.. Maybe I should write a book on perk/gtk, maybe after I finish this term paper.
Actually, the one I have is an elf binary, but the author of it must be a loyal windows user because he added an .exe extension to the binary file ;p
The obvious one: "Learning Perl" by Randal Schwartz and Tom Christiansen. Published by O'Reilly.
http://www.oreilly.com/catalog/lperl2/
I'm not familiar with "Perl by Example", but when
I want working examples of Perl code, I go to
"The Perl Cookbook" by Tom Christiansen & Nathan Torkington, also on O'Reilly.
(And "Learning Perl" may not be a perfect book -- myself I was a bit put off by the artificial nature of the examples in the first chapter -- but it's hardly "garbage". )
I thought "Learning Perl" was great. At that time (a year ago) I had never used a *nix system, ever. I had tried Perl but was confused by it's Unix slant. Learning Perl On Win32 systems helped me out alot.
It was Perl, more than anything else, that got me into Unix, and hence, Linux.
I got more out of chapter one than I do out of most computer books...
'course I already had a good base in c/c++...
I just installed Perl/Tk yesterday. Got the book (Learning Perl/Tk) earlier today. Wrote a program already. (really just added a GUI to an existing program).
It's a good book, and very informative. I spent all last night fooling with Perl/Tk, and searching the web for good online introductory material. It was slim pickins, let me tell ya.
I got more done after spending 20 minutes reading the book than I did with 4 hours of searching and trial and error.
Combine Perl/Tk with perl2exe, and who needs VB? Now my exact same Perl/Tk program can run on Linux or Win32, and if they don't have Perl installed, I just perl2exe it!
VB who?
What's a good book for someone who wants to start from square one on Perl?
Thanks.
Perl is as structured as you make it. It's up to the programmer. That's the way Larry wants it.
I'll make a wild guess that you're getting burned by some historical confusion concerning Tcl/Tk interfaces used by Perl. Unfortunately, there's a Tcl/Tk package (not likely to be what you want), an ancient Tk package (ditto, surprisingly enough) and what you do want: either Tk400.202.tar.gz, or, possibly, Tk800.012.tar.gz (which is based on the version of Tk used in Tcl/Tk 8).
Of course, it's possible that you do have the right version of perl/Tk, but don't have them installed correctly; re-installation might do the trick there.
Or, it's possible that the perl/Tk tax package isn't giving you useful feedback about what it really wants.
King Babar
Babar
I assume you mean a grid geometry manager, not a widget. Tk has a grid geometry manager. There is also another grid geometry manager available called "table" which is part of the BLT package (www.tcltk.com/blt) that is a bit easier to use IMHO. BLT also has a very complete 2D graph widget which is one of the main reasons I picked Tk for a graphing application I am writing.
Personally, I find Tcl syntax to be a bit more natural than Perl, but they're both useful languages and each has it's strong points. Keep in mind that Tcl was originally designed to be an embedded scripting language for applications. There are some shortcomings, such as way too limited data types and structures, but I have written some medium sized applications (~5000+ lines of code) in Tcl/Tk in much less time than it would have taken using c and a GUI library. BTW, Tk can be used in c, it's just more work.
Anybody who likes Tk should check out the incr Tk / Iwidgets packages. One place to get them is www.tcltk.com. They make life much, much easier when creating any size GUIs.
GUILE-Gtk Rocks!!!!!
I read PERL BY EXAMPLE, by (I think) Sam Medinets. You can find it online at the Macmillan Personal Bookshelf, at http://www.mcp.com/personal.
Take a look at it, it's a great book.
Just read the book in question and loved it.
Having spent the last year with MS development tools such as MFC before learning the PerlTk module available from ActiveState I was enthralled with Perl's syntactic and stylistic flexibility, plus, the blazing speed I can design/build complex event driven GUIs without depending on brittle proprietary Wizard built hocus-pocus code.
I'm not surprised most users/consumers don't seem to respond well to menu driven console programs. PerlTk is a wonderful way to enhance your sysadmin scripts as well, and escape from those convoluted menus and argument lists. All this while you write code that can run across multiple platforms with little or no tweaking. What's not to like?
Nancy Walsh's book _Learning_Perl/Tk_ is supporting evidence for my theory that Perl programmers write better books.
As all files are potential bases for data, and the sheer volume of ASCII text files vastly overwhelms any amount of data accessed via the database apps VB5 optimally targets, I posit, Perl Master-of-Strings that it is, with a Perl/Tk GUI exudes a more complete and universal RADness than VB5. Want to race IDE's now? puurrrrrrr...
Hope you haven't purchased _Perl_Buy_Example. It can be downloaded for free from http://www.rcs.ru:8102/useful/Perl5/noframes.htm
... but, what the heck, no-corporate-body is perfect ). Once you really dig into Perl you may well change you mind about O'Reilly's Perl books.
If you are into all-inclusive tomes then be sure to scope out _Perl5_Complete_ published by McGraw-Hill ( their the ones that like to put corporate ads into the textbooks of elementary school children and then blame it on the companies that purchased the space