Slashdot Mirror


Wicked Cool Perl Scripts

Michael J. Ross writes "Of all the popular programming languages now in use, Perl is perhaps the best suited for writing utilities — for several reasons, such as its text-processing capabilities, ease of addressing system resources, and minimal language overhead for input, output, list processing. It was designed to blend the rapid solution development of shell scripting with the powerful control constructs of third-generation languages. Consequently, Perl quickly became a favorite language for developing programs ranging from system administration utilities to CGI scripts that power Web sites. In fact, Perl has been called the glue that holds the Internet together. The tremendous flexibility and power of Perl is seen in Steve Oualline's book Wicked Cool Perl Scripts: Useful Perl Scripts That Solve Difficult Problems." Read the rest of Michael's review Wicked Cool Perl Scripts author Steve Oualline pages 336 publisher No Starch Press rating 8 reviewer Michael J. Ross ISBN 1593270623 summary 47 useful Perl scripts for Web site management or CGI, Linux or Unix system administration, managing pictures, etc.

Published by the cleverly named No Starch Press, Wicked Cool Perl Scripts comprises 336 pages, spanning 11 chapters, with a brief introduction, as well as an index. The book appeared in February 2006, and was published under the ISBN of 1593270623. No Starch Press maintains a Web page for the book, where readers can find a sample chapter (the third one, covering CGI debugging), in PDF format. There is a link for downloading all of the source code.

The book presents 47 scripts, grouped into 11 categories: general-purpose utilities, Web site management, CGI debugging, CGI programs, Internet data mining, Unix system administration, picture utilities, games and learning tools, development tools, mapping, and regular expression graphing. The scripts perform such functions as finding duplicate files on your PC, converting currencies, processing error logs, generating jokes randomly, getting stock quotes, and managing photos and other images. Some of the scripts play games, while others would be invaluable to any Linux or Unix system administrator. For readers with their own Web sites, the book offers scripts for verifying links, locating orphan files, detecting hackers, and locking them out. In addition, there is a script for counting the number of visitors to your site, and even one for presenting a guest book. Software developers will find the material valuable, as there are Perl scripts for generating code, locating dead code, and handling regular expressions — parsing and graphing them.

The scripts themselves are fairly wide ranging in complexity and size, with a few fitting on a single page of the book, while others require more than ten pages. Fortunately, the scripts generally contain enough comments to be clear in how they work to any programmer comfortable with the language. Nonetheless, the author explains how to run each script, what sort of results the reader should see, how the script works, and what modifications one might want to make to it ("hacking the script"). In addition, every one of the scripts contains a POD (Plain Old Documentation) section, though only in the downloadable version — not the version seen in the book, to save space.

It is doubtful that any beginning Perl programmer might mistake this book for a Perl primer or reference. The title alone makes clear that the focus is on the offered scripts themselves, and their ability to help the reader solve common problems. On the other hand, Perl programmers of any level of fluency with the language would benefit from reading through the scripts, as well as the author's explanation of how they address and solve each problem. I myself have been programming in Perl for ages, and yet I spotted CPAN modules that I can use in my own Perl scripts in the future.

The value of the scripts themselves to each individual reader, naturally depends upon what sort of tasks the reader would like to accomplish with Perl. The 11 categories of scripts are varied enough so as likely to be of use to just about anyone who would like to use the "Swiss Army knife of languages" for getting the job done on their computer, or that of their employer (as a system administrator). Personally I found most useful the scripts for detecting changed files, scanning Web sites for dead links, and parsing regular expressions.

There are other aspects to like about this book. It has a RepKover binding, to lay flat when open. The illustrations are clear and not excessive in number. Unlike some technical authors, whose weak attempts at humor simply make their obtuse material more annoying, Oualline is more subtle, such as his reference to the cost of Microsoft Windows CDs in a Hong Kong shop, or "Ingesting a Cheerio nasally." Well, perhaps not always subtle, but invariably welcome in what could otherwise be an extremely dry subject.

Like any book, there are some areas for improvement, perhaps in future editions: In the illustrations that employ rays pointing from one node to the next, some of the curved rays are remarkably jagged, as if they were not computer-generated. Far more importantly, some of the scripts could benefit from more internal comments, as well as having the code broken up into smaller functions, which improves clarity and maintainability. Also, some of the variables and functions could use more descriptive names. For instance, using two examples from a randomly chosen page: $file_name would be more clear than $cur_file (is it the file's name, full path, or contents?). print_file_cell() would be better than do_file() (do what to the file?).

But aside from those weaknesses, Wicked Cool Perl Scripts is a fine book that would be of interest to any Perl programmer, regardless of their expertise. In fact, the administrator of a Web site or a Linux/Unix server, would not even have to know the language in order to download these Perl scripts, and use them to solve problems on the job.

Michael J. Ross is a freelance writer, computer consultant, and the editor of the free newsletter of PristinePlanet.com.

You can purchase Wicked Cool Perl Scripts from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

12 of 239 comments (clear)

  1. Re:Good Practice? by Cleon · · Score: 5, Insightful

    Admins that download scripts off the Net without even checking to see how they work are a danger to themselves and others.

    --
    Gifts for Geeks - Stuff that really matters!
  2. yes and no by JeanBaptiste · · Score: 5, Insightful

    While it is important that the Admin completely understands what is going on.... theres no need to re-invent the wheel if someone else already went through the trouble of writing and testing it.

    1. Re:yes and no by finkployd · · Score: 5, Funny

      Personally, I don't think you truly understand how a wheel works until you reinvent one :)

      Finkployd

  3. Perl? Bah! by Anonymous Coward · · Score: 5, Funny

    Anyone worth their salt would use an AJAX web 2.0 implementation of ruby on rails with a J2EE backend running struts marked up with DXHTML all bound together with an XML-SOAP web service to do their system administration via a proxied web cache. Changing configs would just involve editing a little CSS.

  4. Re:Solve it by Anonymous Coward · · Score: 5, Insightful

    "There's more than one way to do it" should not be considered a personal challange to find them all

  5. Re:Good Practice? by drinkypoo · · Score: 5, Insightful
    In fact, the administrator of a Web site or a Linux/Unix server, would not even have to know the language in order to download these Perl scripts, and use them to solve problems on the job.
    Is these really that good of a practice though? Your pc's will be jam-packed with go you never wrote ... therefore you don't know what's actually going on with your own machines? Write your own scripts script kiddies.

    Let's try this from another angle.

    "In fact, the administrator of a Web site of Unix/Linux server would not even have to know the language in order to download Apache, compile it, and use it to serve pages."

    "Is this really that good of a practice though? Your PCs will be jam-packed with software you never wrote ... therefore you don't really know what's going on with your own machines. Write your own programs, kiddies."

    (I corrected your spelling, grammar, and punctuation errors as well.)

    Basically, your argument amounts to absolutely nothing, because it's no different from other programs. Do you REALLY think that admins typically vet every line of code on their systems? People don't live that long. Know what the difference is between a C program you don't understand, and a perl script you don't understand? The C program is compiled once, and the perl script is JIT-compiled ever time you run it.

    --
    "You're right," Fisheye says. "I should have set it on 'whip' or 'chop.'"
  6. Re:Solve it by Abcd1234 · · Score: 5, Insightful

    Perl is the most wonderfully architected, elegant, flexible language in the world. It's like a fully stocked kitchen with everything you'd ever need to get the job done, and more. Any new version of Perl would be a step backwards.

    See, we can both make absolutist, arbitrary statements with no basis in reality. Fun, eh?

  7. Perl praise and beefs interspersed by tinker_taylor · · Score: 5, Insightful

    A humble Perl user's thoughts on this brouhaha --

    Praise
    =======

    1) The power of perl is irrefutable -- it helps slap together quick and clean solutions to irritating admin problems. The flip-side of being a perl jockey I guess is that one tends to try and create a solution to many a problem that already has a solution - because searching CPAN can be a pain at times.

    2) Use of the more flexible features of the languages (such as Hashes, hash of hashes etc) data/number munging and organization becomes more manageable.

    3) Using Perl's almost endless modules, a lot of relatively complicated tasks can be simplified.

    4) Annoyance factor of numerous tasks (especially Administrative and reporting) can be reduced drastically with the help of Perl.

    Beefs
    =====

    1) The beef I guess is that unlike Python or Perl's other competitors, Perl modules don't come tightly integrated with the core distro. Agreed that Perl probably has a lot more modules than any of those other languages do, but a larger than ordinary de facto distribution (why not include important modules like Digest::MD5, Crypt modules, SSH modules etc?) would be desirable (especially in those situations where you don't have access to the internet directly from within corporate networks and can't install the modules with the "perl -MCPAN -e shell" option) . There might be those Perl veterans who would say -- "build your own distro with your custom modules already packaged" -- and while that might be a very smart thing to do, many a time (when one keeps moving from one environment to another -- some call it job hopping, it helps to be able to download one single perl distro package or rpm or the source+compile and have basic administrative scripts work -- especially those that rely on centralized automation (ssh-based trusts, copies across the network, etc).

    2) Also, perl's syntax can be terse and difficult for noobies to understand (or even older perl-hands for that matter -- when someone has written code without appropriate comments, etc).

    3) Tinkering with Python recently, I found it's simplicity refreshing and it's syntax easier to comprehend (especially when compared with Perl's (imho) complicated "scoping" requirements, etc).

    4) Sometimes (and I guess it depends on the person writing the code) Perl tends to over-complicate things that can be easily handled via Shell scripts.

  8. Re:Solve it, Fixed version by rgmoore · · Score: 5, Interesting

    That looks as though you don't know Perl very well. There's no need for a separate main routine, as whatever is in the file that's not part of another subroutine is assumed to be part of main. There's also no need to have the program end in a true statement (that's necessary only for modules) or to use an __END__. In the true spirit of Perl (i.e. eliminating needless elements) here's a refined version:

    #!/usr/bin/perl

    =head1 GOAL
    Program -- Solve it -- Solves the worlds problems.
    All of them. At once.
    This will be a great program when I finish it.
    =cut

    use warnings;
    use strict;

    #Do something here

    exit;
    --

    There's no point in questioning authority if you aren't going to listen to the answers.

  9. Re:Solve it, Fixed version by eln · · Score: 5, Insightful

    The exit statement at the end is unnecessary.

  10. Re:Now that is a cool poll by audreyt · · Score: 5, Funny

    ...and I would be inclined to agree. :-)

  11. As much as I hate granting time to the Perl haters by Borf · · Score: 5, Insightful

    I'm giving in this time.

    I work in a shop where we maintain (after last count) 112,002 lines of perl in a single system (which also contains about half a million lines of C).

    Guess what? It's not a problem! Not in the slightest!
    And you know why?

    - Modules
    - Coding conventions
    - Mature programmers

    Two of those three are redundant. Take a guess which ones (the third item isn't part of the anwer set).

    If you take a programmer that writes disciplined, careful, extensible, extendable and professional C - are they going to start generating hacked up crap when they switch to Perl? No. They're not. They split source among modules. They use naming conventions. They use strict. They use the namespaces. They use clear syntax. The end result looks almost like C most of the time. Except when it doesn't, 'cause it's Perl.

    What does C written by hack-job Perl "programmers" look like?

    Rephrasing #37 - "It ain't the arrow, it's the (Native American)".