More Advancements in Perl Programming
by Simon Cozens
|
Pages: 1, 2, 3, 4
Testing
Advanced Perl Programming showed how to write tests so that we all can be more sure that our code is doing what it should. How do you know your tests are doing enough? Enter Paul Johnson's Devel::Cover!
Devel::Cover makes a record of each time a Perl operation or statement is executed, and then compares this against the statements in your code. So when you're running your tests, you can see which of the code paths in your module get exercised and which don't; if you have big branches of code that never get tested, maybe you should write more tests for them!
To use it on an uninstalled module:
$ cover -delete
$ HARNESS_PERL_SWITCHES=-MDevel::Cover make test
$ cover
This will give you a textual summary of code coverage; cover -report html produces a colorized, navigable hypertext summary, useful for showing to bosses.
This ensures that your code works--or at least, that it does what your tests specify. The next step is to ensure that your code is actually of relatively decent quality. Because "quality" is a subjective metric when it comes to the art of programming, Perl folk have introduced the objective of "Kwalitee" instead, which may or may not have any bearing on quality.
All modules on CPAN have their Kwalitee measured as part of the CPANTS (CPAN Testing Service) website. One way to test for and increase your Kwalitee is to use the Module::Build::Kwalitee module; this copies some boilerplate tests into your distribution that ensure that you have adequate and syntactically correct documentation, that you use strict and warnings, and so on.
All of this ought to go a fair way to improving the Kwalitee of your code, if not its actual quality!
Inline
One of the things that has come over into Perl 5 from Perl 6 development is the concept of the Native Call Interface (NCI). This hasn't fully been developed yet, but chromatic (yes, the editor of this very site) has been working on it.
The idea is that, instead of having something like Inline or XS that creates a "buffer" between Perl and C libraries, you just call those libraries directly. At the moment, you need to compile any XS module against the library you're using. This is particularly awkward for folk on cut-down operating systems that do not ship a compiler, such as Palm OS or Windows.
The strength of NCI is that it doesn't require a compiler; instead, it uses the operating system's normal means of making calls into libraries. (Hence "Native Call.") It uses Perl's DynaLoader to find libraries, load them, and then find the address of symbols inside of the library. Then it calls a generic "thunk" function to turn the symbol's address into a call. For instance:
my $lib = P5NCI::Library->new( library => 'nci_test', package => 'NCI' );
$lib->install_function( 'double_int', 'ii' );
my $two = NCI::double_int( 1 );
These lines find the nci_test shared library and get ready to put its functions into the NCI namespace. It then installs the function double_int, which is of signature int double_int(int) (hence ii). Once this is done, you can call the function from Perl. It's not much trickier than Inline, but without the intermediate step of compilation.
NCI isn't quite there yet, and it only supports very simple function signatures. However, because of its portability, it's definitely the one to watch for Perl-C interfaces in the future.
Everything Else
The last chapter is "Fun with Perl." Now, much has happened in the world of Perl fun, but much has happened all over Perl. There were many other things I wanted to write about, as well: CPAN best practices for date/time handling and email handling, Perl 6 and Pugs, the very latest web application frameworks such as Catalyst and Jifty, and so on. But all these would fill another book--and if I ever finished that, it too would require an update like this one. So I hope this is enough for you to be getting on with!
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 7 of 7.
- HTML::TreeBuilder ???
2006-12-14 00:20:46 GrantMcLean [Reply]
Interesting article, but I can't fathom the recommendation to use HTML::TreeBuilder. If you want to parse HTML or XML then XML::LibXML with its XPath support, is pretty hard to beat.
- Different title, maybe
2006-01-30 20:08:03 freelancer [Reply]
I honestly found, and still find, the first 'Advanced Perl Programming' more useful as a book about, well, advanced Perl programming. I was initially disappointed with the second edition until I saw it for what I think it really is: "What To Do When You Don't Want To Mess With Advanced Perl Programming". When I look at it that way, it's great.
This article was a great addition, too. Thanks.
But still, when I DO want to do advanced programming, tossing together third-party code and creating all kinds of dependencies isn't really want I'm looking for -- I'm looking for how to create that code.
- Different title, maybe
2006-01-30 20:04:30 freelancer [Reply]
2006-01-27 01:45:55 MichaelStepanov [Reply]
Nice article! It was very useful for me, expessially about Class::Accessor. It's amazing! It can be used together with Class::DBI.
But regarding Templating whay you don't want to write about Embperl (http://perl.apache.org/embperl)? It's simpler than Mason but it allows use basic functionality (Embperl) to develop simple site and extended Embperl::Object which has similar features like Mason. I think it will be useful for Perl developers to know about new template system.- EmbPerl
2006-01-29 01:01:04 DaveCross [Reply]
Michael,
Firstly, EmbPerl isn't a new templating system, it's been around for a long time - in fact it was one of the first templating systems for Perl.
As for why Simon doesn't cover it, well as Simon says "the world seems to be moving away from templates that include code in a specific language (say, Perl's) towards separate templating little languages, like TAL and Template Toolkit."
If you want a templating system where the template language is Perl then HTML::Mason seems to be most people's choice. I can't really see what advantages EmbPerl would have over that.
Dave...- EmbPerl
2006-01-30 02:44:07 MichaelStepanov [Reply]
I don't agree with you, Dave. Embperl has at least one big advantage to Mason. It's a division into simple framework (Embperl module) and complicated (Embperl::Object). To create a simple web site (home page, for example) it'll be enough Embperl. To develop some enterprise site it'll be suit Embperl::Object. Mason doesn't have such division and it's complicated framework. Developer should spend a lot of time to understand it functionality.
About template systems I agree with your opinion but there are many problems when you try to use them.- Mason
2006-02-07 20:35:56 namotco [Reply]
Being new to web development I found Mason sensible and straight forward. To make a simple site (even a home page) takes <15 minutes of reading the syntax. Mason will scale well for a more complex site if you know more about it and use the more advanced methods and techniques.
Perhaps I've missed something about TT or EmbPerl, but after reviewing my options I chose HTML::Mason.
(Thanks for the great modules Dave...)
- Mason
- EmbPerl
- EmbPerl



