Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
×
Perl Books Media Programming Book Reviews

Writing Perl Modules for CPAN 63

chromatic writes with the review below of Writing Perl Modules for CPAN, which explains at a level "between novice and intermediate user" (and in a minimum of space) how to contribute to Perl's own Library of Alexandria.
Writing Perl Modules for CPAN
author Sam Tregar
pages 288
publisher Apress
rating Recommended.
reviewer chromatic
ISBN 159059018X
summary A guide to the use and production of Perl modules, from start to finish, in C and in Perl.

The Scoop

Besides Perl's abilities as a rapid development language, it's widely believed that the CPAN is its most valuable feature. This network of freely distributable code allows competent developers to achieve great heights of productivity, reusing the work of a generous community of programmers.

Of course, just as some will argue that Perl's copious documentation (spread over two thousand pages) is not immediately obvious to beginners, neither is how to use and even to contribute to the CPAN. For every coder who's successfully published a module, how many more would jump at the chance? How many registered CPAN authors would like to improve their skills?

With that audience in mind, Sam Tregar's Writing Perl Modules for CPAN plants itself firmly in the gap between novice and intermediate user. While much of the book presents information present in a multitude of FAQs, manpages, and the bittersweet experiences of those of us who did things the hard way, he's collected much knowledge into a short and readable guide.

What's to Like?

Tregar starts by describing the history and usage of the CPAN itself. This includes the three most popular approaches to building modules: through the CPAN shell (including its configuration), by hand, and with ActiveState's PPM tool. Next, he explains module development in forty pages. This is pretty dense stuff for the intended audience and might require several passes by newer coders. Only after re-reviewing the chapter for this summary did I realize how much he covered. The next chapter covers design and style, from naming schemes to appropriate laziness through code reuse. It's more philosophical and more important.

The next two chapters cover bundling and submitting modules to the CPAN, as well as being a good author and maintainer. The general tone is quite similar to the impressive Open Source Development with CVS. While manpages usually describe the mechanics of making a distribution, for example, they rarely explain the reasons why things are done that way. As with previous chapters, several code examples illustrate the concepts under discussion.

After a brief chapter discussing a few very effective CPAN modules, Tregar dives into XS (the interface between Perl and C). In 60 pages, he describes just enough of XS and the Perl API to teach careful programmers how to be effective at extending Perl. This introduction compares favorably to the first few chapters of the new (and excellent) Extending and Embedding Perl. As expected in an overview, he provides links to more information. The writing and example style is clear enough that a decent coder with sufficient C knowledge should be able to write a Perl wrapper to a C library with relative ease.

The last two chapters describe Inline::C, an abstraction layer that makes XS much easier, and CGI::ApplicationC, a state machine framework for Perl CGI applications. It's not quite clear why the last chapter was included (besides Tregar's desire to see more CPAN modules extending CGI::Application), but it serves as an example of using and extending a CPAN module. Perhaps a future version of the book will elaborate further.

What's to Consider

The book's code samples are generally good. In the first half, they are all related parts of a larger project. The rest of the book moves away from this approach. Perhaps it would have been worthwhile to continue the theme, though the nature of the material makes it difficult to see exactly how to accomplish this.

Tregar also avoids the use of strictures and warnings in his code examples, claiming that they would make the examples too verbose. I disagree with the given reasoning -- teaching is the best time to enforce good habits, especially when encouraging the students to distribute their code to the world. This is a minor issue, though, as the code is readable and reasonable.

In the past few months, two projects have gained a great deal of momentum in Perl space. These are the CPANPLUS (disclaimer: I am contributing to this project and have contributed to CPAN.pm) and Module::Build. They may become the new standards, replacing CPAN.pm and MakeMaker as early as Perl 5.10. The book omits mention of these. This is understandable, given the time frame -- and the current tools will not be disappearing any time soon. Potential replacements for h2xs are described in a sidebar, though.

The Summary

This is a readable book. It took only a couple of hours to read (though I'm assuredly not the target audience), and is well packed with good advice. Fresher Perl programmers who aren't yet comfortable enough with packages and interfaces will get the most benefit, but there's plenty of information for intermediate hackers as well.

Table of Contents

  1. CPAN
  2. Perl Module Basics
  3. Module Design and Implementation
  4. CPAN Module Distribution
  5. Submitting Your Module to CPAN
  6. Module Maintenance
  7. Great CPAN Modules
  8. Programming Perl in C
  9. Writing C Modules with XS
  10. Writing C Modules with Inline::C
  11. CGI Application Modules for CPAN


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

This discussion has been archived. No new comments can be posted.

Writing Perl Modules for CPAN

Comments Filter:
  • by Anonymous Coward
    Considering that the upcoming version of Perl will supposedly not be 100% backwards compatible with Perl 5, what happens to CPAN? Will it be ported en masse to Perl 5, or left the way it is as programmers embark on a new CPAN?
    • by jbolden ( 176878 ) on Monday September 23, 2002 @10:50AM (#4311512) Homepage
      Perl 6 will have a Perl 5 compatability mode; which means that Perl modules (especially those which are giving high level functionality) written for 5 will load in this compatability mode and still work fine. Those giving lower level functionality will be replaced by Perl 6 modules with a speed increase since the Perl authors will have 10 years more information about what the community actually wants to use Perl for. More importantly the low level Perl interface (Parrot) will be much easier than the current rather complex Yacc interface.

      • by ajs ( 35943 ) <ajs.ajs@com> on Monday September 23, 2002 @11:44AM (#4311978) Homepage Journal
        You slightly over-simplify, but more importantly, you've left some things out.

        Perl 6 will not have a "compatibility mode" per se (actually it will in patterns, but that's not what you were refering to); it will have a full-fledged Perl 5 compiler that will compile down to Parrot byte-code just like Perl 6 will (as will Python, Scheme and some other languages).

        By its nature, it will have to know more about Perl 6's internals than the other language front-ends, but not by much. You will be able to do something like "perl5 program " and your program will run exactly as you expect, and modules will be auto-recognized (because Perl 6 does not use the "package" keyword).

        There is a catch. XS modules (those that directly interface with Perl's internals in order to talk to external C or C++ libraries) may need more help. In most cases, the interface will still be available through some emulation, but some will simply have to be fixed to work with Parrot-based Perl 5.
    • What is not straight away compatible, will have to be converted, but that will be done, in 95% of cases, automatically. Only some quirky regexps will have to be translated by hand (and then, there's a perl 5 compatibility mode for them).
  • I loved this book (Score:5, Informative)

    by andy@petdance.com ( 114827 ) <andy@petdance.com> on Monday September 23, 2002 @11:06AM (#4311641) Homepage
    From my review in the latest issue [theperlreview.com] of The Perl Review [theperlreview.com].
    Sam Tregar's
    Writing Perl Modules for CPAN is an excellent introduction to the world of modules, and the community of Perl and open source software in general. Tregar surveys all aspects of module installation, creation, documentation, testing, packaging, distribution and maintenance in a clear, readable style.
    I don't have any disagreement with anything chromatic has said, and I agree that the important bit of value here is that it's based on experience beyond what docs and FAQs can say.
  • Test! (Score:2, Interesting)

    If you don't know where to start, the best thing is to test and debug existing modules! It's the fastest way to get started on your way to Perl stardom.

    See chromatic's How You (Yes You!) Can Get Involved [perlmonks.org].
  • by ajs ( 35943 ) <ajs.ajs@com> on Monday September 23, 2002 @11:35AM (#4311897) Homepage Journal
    Perl is a wonderful language, but like any it takes time to ease into, and it has its warts (both the aesthetic "ew, I hate dollar signs" warts that newbies hate and the deeper "roll your own OO is painful" blemishes). However, CPAN ends all argument, IMHO. If you aren't using CPAN, you're wasting your (company's?) time.

    There are just so many amazing libraries in CPAN, I can't get over how most of what I want to do is already there, and at my fingertips. Want process management? WWW client emulation? Scientific computing functions? It's all in there, and managable via the CPAN.pm module.

    Come Perl 6, CPAN (or its successor) will be even more valuable. The back-end virtual machine (Parrot) will have modules which give it access to external capabilities, and any Parrot-based language will be able to use them. You'll be able to call Perl's LWP from Python, or a scheme tree-handler from Perl. The details are all being worked on as we speak, but the basic building blocks are mostly in place already.
  • The hardest part (Score:5, Interesting)

    by Ed Avis ( 5917 ) <ed@membled.com> on Monday September 23, 2002 @11:52AM (#4312030) Homepage
    The most difficult part of writing a module for CPAN is getting past the bureaucracy: there are at least two different documents giving different instructions on how to submit modules, and the modules@perl.org mailing list is notorious for never replying to any messages sent to it, especially not those asking for help or 'what went wrong?'. It's not like registering a project on Sourceforge or Freshmeat; you have to send mail to register a namespace, then do an ftp upload and then send mail to have your upload 'noticed'. Sounds reasonable enough, except that it is easy to go wrong or fail to jump through a particular hoop, and if that happens you get no feedback at all. I'm sure the CPAN maintainers are busy and hard-working, but the current situation shows there are cases where an automated system (which at least gives error messages when stuff goes wrong) is better than manual administration.

    Myself, I have managed to put modules on CPAN, after a few months of wondering why they didn't appear in the main listing. (It turned out I had forgotten to send an updated module list entry; my fault, but still it would have been friendlier for somebody to answer my question about what I did wrong rather than just ignore it.) But uploading newer versions of the same modules has sometimes been troublesome, with the update appearing in my home directory but not the main listing: again, any request for help or guidance on what part of the process I was doing wrong was studiously ignored.

    Despite the troublesome upload procedure, CPAN is Perl's biggest asset and other language communities would do well to copy it (as some are). But please don't copy the management system; have some more structured way to submit code where it's clearer what to do at each stage, and there can be warning messages if you do something wrong, rather than just quietly failing to work.
    • I suspect it is a "you must be this tall to ride this coaster" thing.

      I registered as a CPAN author, got my ID, then registered my namespace and uploaded my module. It all took about 36 hours from sending email to register as an author to my module being on all the CPAN mirrors I checked (ego surfing :).

      And all the documentation says wait 2-3weeks for any action to be taken. I am a very happy CPAN contributor.
  • Why doesn't the open-source community find a way to design libraries than *any* language can use (perhaps with a few internal adjustments to make the connection seemless)?

    Is it a hard problem technically, or is it politics?

    I wish to know. I see too much reinvention of the wheel in different languages, and consolidation looks more logical if there are no signficant technical barriers that someone can identify.

    We are going to make Microsoft look good (.NET) if we cannot figure this out. (Although MS may be making some painful sacrifices WRT variety.)
    • There are many reasons to that, but the main is:

      All languages are not created equal.

      One language's junk is another language's treasure.
      • (* There are many reasons to that, but the main is: All languages are not created equal. One language's junk is another language's treasure. *)

        What you are suggesting is that people want the libraries to mirror the philosophy of their favorite language.

        That is fine, but the drawback is having to reinvent the wheel for every language, new and old.

        "I want my wheel my way" is a source of a lot of redundancy in software. I suppose such redundancy may create jobs in some cases, but it just makes the Vulcan area of my brain shudder.

        Plus, perhaps having a larger library is more important than having a library customized for each language philosohpy. There might be an obscure format or feature that you want a component for, and if all that effort is spent on deep libraries rather than language-specific duplication, one could prehaps have that.
    • Let's take a simple function, one that adds n numbers.

      In C, the obvious route is to pass n and the numbers to a varargs function.

      In C++, the obvious route is to pass a vector (from the Standard library) to the function.

      In Ada, the obvious route is to pass an array to the function.

      (Or, with C++ or Ada you could use template/generic functions. Faster, but the disadvantages of C functions.)

      In Lisp, the obvious route is to pass a list to the function.

      Even ignoring C here, to map a vector to an array to a list takes O(n) time, wiping out any advantages to using the function. You can create a basic library with a vector/list type, and interface it to all three languages, but it will be more clumsy than native types. You end up losing some of the advantages of using each language. (This, of course, is the way that .NET works.)

      OTOH, where it's worth it, everyone does use the same library. Look at GTK+; every serious programming language has bindings to it, and it's often the most popular graphic toolkit for that language. Part of what makes that possible, though, is the fact that GTK+ was written in a way that makes that possible; part of what makes it tolerable is that most of those bindings were handwritten, covering the more blatant clashes with GTK and the language. Even then, there's some stuff that can't be fixed; for example, GTKAda has a few problems that no native Ada toolkit would have.
  • by yoz ( 3735 )
    Does the book cover the writing and packaging of a test suite? I'm currently writing a Perl module that I hope to submit to CPAN and writing decent tests is one of the parts that scares me most.

    -- Yoz
  • It took only a couple of hours to read (though I'm assuredly not the target audience)

    Which is exactly the reason why someone else should have reviewed it! Books should be reviewed by someone with the appropriate skill set, that's got about the same amount of knowledge that the books intended audience have. If that isn't the case the review is pretty much useless.
    • I disagree very strongly. If a book's intended for beginners, an expert really ought to make sure it contains nothing misleading or wrong. In this case, since I'm experienced with building and distributing modules, I think I have a good perspective on the process. Someone who's never done it before won't be able to compare the book to personal knowledge and experience.

  • Inline rules! (Score:4, Interesting)

    by Coppit ( 2441 ) on Monday September 23, 2002 @03:50PM (#4313849) Homepage
    Let me just say, in case you haven't heard it before, Inline kicks major ass. It allows you to seamlessly integrate C, Java, C++, Python, etc. into Perl modules. When you run the usual

    perl Makefile.PL
    make
    make install

    sequence, Inline automatically calls the compiler and linker for your C/etc code, and creates the right glue code between your Perl and C/etc code. For a simple example, see the C mailbox parser which comes with <shameless plug>grepmail [sf.net]</shameless plug>.

    By the way, recent improvements to the Perl implementation mean that my Perl mailbox parser is now less than 5% slower than the C implementation. Just one data point for those of you who say Perl can't be fast. ;)

Two can Live as Cheaply as One for Half as Long. -- Howard Kandel

Working...