The Perl Cookbook, 2nd Edition
The new recipes cover a number of subjects. One of the prominent themes is how to use perl's new unicode support, as well as the new I/O layers feature. The coverage of web programming has definitely been fleshed out with recipes on XML-RPC, SOAP and so on, plus the new chapter on mod_perl. Also of interest of course are the additional recipes on database access with DBI.
The mod_perl chapter is a good succinct introduction, with some very cute recipes in it (though admittedly a lot of these are also covered in the excellent Mod_perl Developer's Cookbook by Young, Lindner and Kobes out from Sams). For example "Transparently Storing Information in URLs" shows how to embed information in any arbitrary position inside a URL. This quickly shows the kind of things you can do with a PerlTransHandler and a PerlFixupHandler. The chapter closes with what looks like a good introduction to "Template Toolkit", which I would probably be very excited about if I wasn't already familiar with the (also discussed) HTML::Mason.
I really enjoyed reading the XML chapter (a subject I'm less familiar with): I predict that you'll find this to be the fastest way through the XALPHABET XSOUP without drowning. For me, this was almost worth the price of the book.
Very little has been removed (hence the page count has gone from 757 to 927), and where I have been able to find a deletion, there are usually very good reasons for it. For example, the first edition takes the trouble to tell us that qr// was introduced in perl 5.005, but the new edition drops the babble about versions there, because for most of us, anything before 5.6 is now ancient history. However, I do miss this particular irrelevant parenthetic aside that's been deleted now:
Remember that the opposite of read is not write but print, although oddly enough, the opposite of sysread actually is syswrite. (split and join are opposites, but there's no speak to match listen, no resurrect for kill, and no curse for bless.)(p.295, first edition, compare to p.323, second edition.)
In general, it's difficult to think of anything seriously wrong with the Perl Cookbook. I might suggest that in some places they fall into the trap of talking about all the ways to do it, rather than just the best ways, (e.g. recipe 7.5 "Storing Filehandles into Variables" seems a bit complicated).
And maybe there are some slight problems with order of presentation, as with the new perl 5.8 feature of "I/O Layers", which is mentioned a few times before it's finally discussed in the beginning of Chapter 8 (though really, it's amazing that there aren't more problems like this: this is supposed to be reference work, and yet it usually works well as a tutorial also).
I've got one big complaint about the 2nd edition though: they changed the numbering of existing recipes! I've been writing code with comments like
# Schwartzian transform. See Perl Cookbook, recipe 4.15and now it turns out I should've been specifying an edition number also. Please: "Cookbook" authors, come up with a numbering scheme that remains invariant with new editions... if you can't always just append to the end of the chapter, there's nothing wrong with tacking another dotted decimal on the end. We're programmers, we can handle it.
And speaking of the "Schwartzian transform" that recipe has a very clear, self-explanatory name "Sorting a List by Computable Field", but in the first edition, there was also a footnote explaining that many people call this the Schwartzian Transform, named after Randall Schwartz, who invented the technique. With this second edition, that footnote has been quietly dropped. Guys, if you're going to carry on a feud, this is really not the way to do it. It just makes you look bad.
O'Reilly's perl.com site has a series of articles by the authors, featuring some recipes from the book:
Appendix: New recipes and examples (not including the two new chapters):
- Using Named Unicode Characters
- Treating Unicode Combined Characters as Single Characters
- Canonicalizing Strings with Unicode Combined Characters
- Treating a Unicode String as Octets
- Properly Capitalizing a Title or Headline
- Constant Variables
- Implementing a Sparse Array
- Creating a Hash with Immutable Keys or Values
- Matching Nested Patterns
- Writing a Subroutine That Takes Filehandles as Built-ins Do
- Storing Multiple Files in the DATA Area
- Reading an Entire Line Without Blocking
- Treating a File as an Array
- Setting the Default I/O Layers
- Reading or Writing Unicode from a Filehandle
- Converting Microsoft Text Files into Unicode
- Comparing the Contents of Two Files
- Pretending a String Is a File
- Working with Symbolic File Permissions Instead of Octal Values
- Writing a Switch Statement
- Coping with Circular Data Structures Using Weak References
- Program: Outlines
- Overriding a Built-in Function in All Packages
- Customizing Warnings
- Writing Extensions in C with Inline::C
- Cloning Constructors
- Copy Constructors
- Saving Query Results to Excel or CSV
- Escaping Quotes
- Dealing with Database Errors
- Repeating Queries Efficiently
- Building Queries Programmatically
- Finding the Number of Rows Returned by a Query
- Using Transactions
- Viewing Data One Page at a Time
- Querying a CSV File with SQL
- Using SQL Without a Database Server
- Graphing Data
- Thumbnailing Images
- Adding Text to an Image
- Program: graphbox
- Turning Signals into Fatal Errors
- Multitasking Server with Threads
- Writing a Multitasking Server with POE
- Accessing an LDAP Server
- Sending Attachments in Mail
- Extracting Attachments from Mail
- Writing an XML-RPC Server
- Writing an XML-RPC Client
- Writing a SOAP Server
- Writing a SOAP Client
- Program: rfrm
- Using Cookies
- Fetching Password-Protected Pages
- Fetching https:// Web Pages
- Resuming an HTTP GET
- Parsing HTML
- Extracting Table Data
You can purchase The Perl Cookbook, 2nd Edition from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
Referral: Amazon has this book for the same price as bn and with free shipping
The second edition works great for Perl 5.8. but not for Perl 6 which is going to require a rewrite of ALL of the Perl books.
O'Reilly addresses the issue here
Still Perl is such a beautiful thing you should buy ALL Perl books.
Die windows! DIE!
It doesn't tell you how to build the kitchen sink
Rus
Cheap UK and US VPS
thx.
One of the goals of Perl 6 is to make non-trivial projects possible. That's good. The way it's being done is bad. Perl was once a lightweight, extremely flexible language. Now it's become a huge ugly monster. People wanted OO, so a nasty hack was bolted on top to allow some semblance of it. Now this nasty hack is being expanded. Sure, the code's different, but the basic form is the same. Kludge upon kludge upon kludge; I'd much rather have a nice, clean, pure language (and not one with loads of irritating whitespace thank you very much).
The same goes for the syntax. All the switching between $, @ and % is really irritating (ask a newbie how to get at the length of the keys array of a hash inside a hash, for example), and the changes proposed for 6 are just making this worse -- it seems that Larry, in his infinite wisdom, wants to prefix every data type with a different hard-to-type character. Perl was only designed for the three data types, and adding more is a mess.
Perl 6 is a complete rewrite, but it keeps all the mess which has accumulated over the previous versions. This is not good. Sure, my const int $var = 27; may look neat (in the same way that, say, Pascal does), but $var isn't entirely constant, or entirely an integer, it's just a hack which makes it sort of behave like one. The whole thing is an exercise in pseudo-computer science masturbation with little real purpose except to please the managers who dislike the one thing that makes Perl special.
On a similar note is regexes. I'm an avid fan of regular expressions simply because a nondeterministic finite automata is far more flexible than linear code. However, Larry must have been smoking that cheap $2 crack when he wrote this. Does he want Perl 6 to be flex or something?
I won't be going on to use 6. It's a nice idea, but it's completely unnecessary. It won't make large projects any easier to manage (the language is still, at heart, an almighty hack -- an impressive one, but still a hack). It won't make OO any cleaner. It won't make development any faster. To put it bluntly, Perl scripts will still look less beautiful than our friend Mr Goatse. I'd prefer to use a language which has always been pure synthesis of science and engineering, not some half-baked imposter.
Perl 6 will be nice, but I'm guessing it will be the end of Perl. It can't do what it wants to do whilst still being based upon a nasty mess. There are now other options, which provide all of Perl's power and none of the mess. Sorry, but *BSD^H^H^H^H Perl is dying. Larry is buggering it up the ass without lubricants, just like Shoeboy is doing to Larry's daughter.
So, I've got perl scripts which may need to work on Unicode and non-ascii character sets somtime in the future. Today, I just test them with UTF-8, and it seems to work, but who knows if they really do work on those funny character sets? I can't read Kanji or other non-ascii characters. How can I test to see if they do work? Ideally, I'd like to have an xterm-like window that uses my standard keyboard to generate analogs to all my ASCII characters, but with some test encoding that puts them into some 16 bit character set range. This way, I can make sure that since we've gone to all this work to add Unicode support into our scripting languages that it does indeed work. Does anyone know how to test this?
Thank you. He's insulted quite a few people in his post.
Some people are good at learning stuff by heart. I am not one of them. As a result, I've neither been good at (bio)chemistry (that's why I became a physicist) or at learning the syntax of regexps. To me Perl code looks like line-noise you used to get when you used a modem on a bad line. It's the same thing with Emacs' controls. There's just no logic in it.
So, my question is: how the hell do you remember all those codes (perl, emacs, chemistry,...)?
BOO! TERRO
Wow, I didn't know GOD had a slashdot account. I bow before thee, oh Great One.
Why not do this in the first place? Anyone who's written a university essay knows the importance of edition numbers. Page-numbering is particularly vulnerable, since even small clarifications will shift things around.
A different, and likely just as effecitive alternative, is to not specify the recipe number at all. Surely there's an index where you could look up "Schwartzian transform" without hard-coding the number.
you must have you head so deep in the bits of the subsystem that I am surprised you raised your head to read this article... geez
illustrated in the following holy snippet..
Just trying to save you from eternal damnation
- It's easy enough to get started, but takes years to get really good
- Doing it fast doesn't mean doing it well
- It's the little details that continue to amaze you
- You don't learn it from a book
Remember to actually use perl if you want to experience the bliss of perl.I have found www.perldoc.com quite useful for multiple versions of perl.
I haven't bought a technical book since I have moved out of the States - O'Reilly's Safari has been nice for that. I can still have access to their books.
I haven't looked to see if this new one is on there yet - I have the old version of it in paperback form as I do with pretty much all the other O'Reilly Perl books.
There are some odd things afoot now, in the Villa Straylight.
I moderation , the Slashdot . of how the believes enlighten system in place am greatest political troll someone very at as is ? confuses a system me in curious about unfair It who demonstration Democracy ever .
Lol!
But you got it kinda wrong, Christians are logical only when they want to be. They resolve conflict in their heads using some form of doublethink. Actually everyone does this but Christians are the best by far. And Muslims.
Don't forget Jews!
-1? Do no moderators have a sense of humour?
Very little has been removed (hence the page count has gone from 757 to 927), and where I have been able to find a deletion, there are usually very good reasons for it.
Correct me if I am wrong but I seem to remember that the first edition had quite a few errors (both syntactic and semantic). I'm guessing that the second edition has cleared all these up. However, when I try to access the errata for the first edition I can't find it! Surely support for previous editions doesn't "end" upon publication of newer editions. It is O'Reilly afterall...
I know it's flamebait, but I would point out that one thing that Perl and Python let you do that sed/awk/grep don't is deal with networking. There once was a project to reimplement all the standard Unix utilities in Perl. Without debating the wisdom of doing so, I would like to know how you could implement an FTP server in awk.
On the other hand, I'll admit that I never learned Perl because everything that I needed to implement could, indeed, be done with awk and sed--they are quite powerful for text processing, but it's important to realize that they have limitations.
mmmm... tasty
camelsteak!
hump-b-que!
Guy obviously doesn't know the difference between a Scripting and Programming language.
of the cookbook. Hell, keep the electronic version in a text file somewhere. There have been at least a couple of times I've finished writing a script, then I've looked in the Cookbook and found a better-written solution.
It's unique in that it doesn't just tell you about specific language properties. It actually gives a problem, a solution, and an explanation.
That would be the Perl Power Tools project.
Regardless of the religious wars over languages, I have time and again found Perl to simply be the most practical language for the job and continue to enjoy using it for a variety of tasks and watching it mature.
There is no substitute for the CPAN or Perl's ability to get the job done quickly and easily with an active community providing free (and very effective) support. Both Perl and the Perl Cookbook are an integral parts in my toolkit and for anyone who uses it, whether from time to time or regularly, I recommend it.
-- My choice of computing platform is a symbol of my individuality and belief in personal freedom.
No more FUCKING PERL books! Larry is GAY!
But I guess it's convenient to think of everything that happens as being the result of a feud and ignore inconvenient facts like the "Schwartzian Transform" name still being mentioned in the long entry on sort function in Programming Perl, 3ed, which Tom also cowrote. Hmm, maybe I shouldn't point that out--now I'll be accused of having a feud with Randal!
--Nat
I dont know about FTP but here is a http server written in awk.
Enjoy!
I've tried lots of templating schemes with perl-based web development, but Apache::ASP is my favorite so far.
With the XMLsubs, $Request, $Response, $Server, $Application and $Session objects I am able to focus on the task at hand, rather than figuring workarounds to the limitations of the language I am using.
Global variables, cached database connections & queries, shared session-state and full access to the Apache API give you the power to do anything you need to.
As you would have guessed if you heard me speak in 2000, I'm not the biggest XML user. I've mellowed since then, but I still don't do a lot of XML hacking. (One of the spare-time hacking things I've while here at O'Reilly, though, is to get our internal database of "what books are at what stage" into XML for easy grepping and reuse).
Of all my work in the 2nd edition of the Cookbook, the XML chapter is the one I'm proudest of. I'm really glad you like it. Thanks!
--Nat
I was amazed when I first read about tchrist's power tools project. Thanks for the link. I noticed that all the dates were from '98/'99. Did someone declare it done, or is limbo?
- doug
I've worked as a Perl programmer for several years, and the only book I've ever needed is "PERL - from the ground up" by Michael McMillan.
It has among others a very handy reference to Perl's built-in functions, lots of regexp stuff and more.
For everything else there is www.
Will code a sig generator for food
FYI. Tom Christiansen (54829) used to post comments here. He stopped because of the crap associated with the story on the death of Richard Stevens. Search for Christiansen to read his parting words.
IIRC, then came karma.
What are you on about? One of the oft-heard criticisms of Perl 6 is that it's TOO structured. Just read the Apocalypses and Exegesises (correct my latin plurals here, if you like) and you'll see Perl 6 is really hot on structure.
/C++/Pascal?
The whole thing is an exercise in pseudo-computer science masturbation
You talk about it as if Perl operates on mysterious brand-new concepts. It's a pragmatic language. None of it is particularly unique to Perl. In fact, if you search Google for 'pragmatic programming' the Google directory link at the top is to Ruby, the language you seem to jerk off to.
All the switching between $, @ and % is really irritating
More irritating than having to jump several pages up your code to see variable declarations as in C
I'd rather know @blah is an array, and $blah is a scalar, than have to jump around code looking for ints, chars, and arrays. Of course, you could argue that we should be using a naming scheme for variables. But if so, that invalidates your point too. The @, $, % symbols are, semantically, the same as having variables called i_myint, a_myarray, etc. How hard is to learn a few symbols, anyway?
Face it, you're anti-Perl, and that's okay, but it doesn't make you the most objective person to say which features are worthwhile or not.
Web Hosting Reviews
It'd be pretty easy in GNU awk, which is the most advanced awk out there.
Jurgen Kahrs implemented socket IO in GNU awk around 1997. See Robbins' O'Reilly book "Effective Awk Programming" for more details, it's a great book (even though I think the name is lousy).
If the number of FTP commands you wanted to implement is K, I'd guesstimate you'd need about 30 + (K*2) lines of Gawk to implement a reasonably bulletproof FTP.
Gawk ships with most linux distros, but many of them are shipping outdated versions.
You N00B!!!
They were able to do this because they used inetd to listed to the port for incoming connections. I don't think it is possible to write a stand-alone http server in awk, as that would require some mechanism for listening to ports. If you add in Bash, you can make outgoing connections using built-in special files /dev/tcp/host/port, but I don't think Bash lets you listen to a port for incoming connections (I may be wrong on that).
The first edition was an essential purchase for me, but I find that I refer to the HTML version on the 'Perl CD Bookshelf' much more often than the paperback. Anyone who doesn't subscribe to Safari and wants to read the electronic edition of Cookbook 2 will have to wait until January, when it'll be included in the 4th edition of the Bookshelf.
The first edition had a bug, where it could corrupt your db file. I actually used that until I noticed the warning in the DB_File manpage.
Speaking of manpages, I'm still using the old DB_File instead of Berkeley.pm because that one has such crap documentation!
(:alternation "foo" "bar" (:sequence "b" (:alternation "a" "uz") "z"))
I agree that this does look a whole lot better than the standard PCRE string but, the end result is still the same. It is illogical. There is no intuitive meaning that I can draw from the line. In other words, while it looks better it is still as dumb founding as PCRE.
One advantage of Perl (and other apps/languages with loosely Perl-compatible regular expressions, like Python, libpcre and KDE) is that its regexes are consistent. To get non-magic behaviour, you put a backslash before punctuation, or no backslash before a letter/number; to get magic behaviour it's the other way round. You can turn an arbitrary string into a regex literal by inserting a backslash before any non-alphanumeric character.
Compare with, say, GNU grep: () isn't magic, but \(\) is; unless of course you're in egrep mode, in which case it's the other way round. \< and \> are magic regardless, and <> aren't, but *^$ are magic and \*\^\$ aren't. Suddenly turning strings into regex literals requires in-depth knowledge of precisely which characters are "magic".
(If you're doing something non-trivial, it's also handy having C-style flow control, and being able to evolve your "quick hack" Perl one-liner into a full script with functions, modules and stuff.)
Most of the time when learning a new language, (Perl is no exception), to get your hands on source you usually have to turn to the GPL community. I love GPL, but hate the fact that no one seems to understand the importance of good comments. If there were more (and better) comments, the language might not seem like line noise to novice programmers.
Flexible bare-metal recovery for Linux/UNIX
-B
Ash and Hickory, straight-grained and true, make excellent bludgeons, dandy for the cudgeling of vegetarians.
I'm rather partial to R. Schwartz's books on the topic, the "pearl" book, the "camel" book -- I've never been a fan of tchrist and the documentation he writes. I secretly suspect him of authoring the awful OOP chapter in the Camel book.
Back in the day the venerable "Perl 5 HOWTO" was the shizzy for doing stuff before there was a billion modules in CPAN to do pretty much everything. I still refer to it when I need to write a server from scratch, even though its probably five years out of date now.
To the author who was confused by regular expressions: Jeff Friedel's book on the subject is excellent.
http://www.cgisecurity.com/lib/
http://www.cgisecurity.com/development/
Sure, Laziness is a virtue and all that, but to cite a specific example, I looked up how to parse program options. All Cookbook has to say on the matter is to use GetOpt. This is fine, and is decent advice, until you realize that GetOpt is by no means a complete solution, as it doesn't handle special characters. I beat my head on this for too long before having to search around and learn a graceful way to parse @ARGV (being new). I would have appreciated a reminder that you don't have to stick with GetOpt, and there is in fact, MTOWTDI.
Lesson having been learned, I would like to save other neophytes the trouble.
Fighting the War on the War on Drugs.
http://smokedot.org/
I've (un)successfully used this to avoid learning both perl and python for some years. D'oh!
--
I understand that it uses inetd, but it is a very impressive feat of awkish skill to pull this off. It makes a great use of inetd, but you are right awk has no way of making network connections as its *only* (please dont kill me) a text parser. Inetd is arguably much more efficent then having to code by hand the networking parts of your program. I'm no perl expert but i would say a shell script and inetd would possibly make a faster web server than some perl script, but then again what do i know?
....But I can personally attest that Christiansen can be such a jerk that he can't even be approached when he is positively wrong. I pointed out a very specific bug in Perl back in the Perl 4 days which had not been fixed for weeks to him. He said it had been fixed and that I was a fuckwit for bringing it up again. Gave him code to show I could still do it. He called me a liar. I appealed to higher gods. He caught wind. Then started a campaign to squash me on comp.lang.perl.misc. That worked because of his unfailing followers. His parting shot to me was some shit about being more educated than me (seemed very proud to be able to speak a couple of languages other than English. This is an accomplishment for an American, I admit) and making money at Perl. Blah. Blah. I seriously thought this was someone spoofing the guy for the longest time, because when I met him at conference before this period, he was very forthright and pleasant. Actually, he seemed like a nice guy to hang with or maybe work for. Anyhow, I gave it a rest. Perl is just a fucking programming language. Linux is just an OS. I understand he has disappeared from USENET as well because of abuse from users, his unabiding dislike of Newbies, and lack of time in general. Anyhoodles, all programming languages and OSes suck. Some just suck less. Perl is one that sucks less. Linux is one that sucks less. Hrmph.
Comparing it to Windows will be a moot point, since El Dorado is going to have a 40% larger code base than XP.
You mean, in case Slashdot gets slashdotted?
Ignorance killed the cat. Curiosity was framed.
Inetd is arguably much more efficent then having to code by hand the networking parts of your program.
Maybe if you're arguably retarded. The inetd model is rather inefficient. Thanks for playing, though.
I wasnt talking about speed efficeiency.