in reply to Wicked Cool Shell Scripts implemented in Perl
I cannot reccommend "Wicked Cool Perl Scripts", and this brief thread tells me "Wicked Cool Shell Scripts" is no better.
The title of the book appealed to me. I was hoping to find something along the line of Randal Schwartz's columns. I wanted a pithy page or two explaining some language features. Accompanying it would be a compact and efficient block of code achieving a useful result. Instead, I found something more along the lines of "Matt's Script Archive".
Not only are the scripts of dubious value, but the code is mediocre, rather than presenting an ideal to which we should aspire. Some code is actually wrong.
For example, in the first chapter, while discussing File::Find, Steve Oualline returns undef to indicate there are no values to return. It would be absolutely correct if used a return statement on it's own:
return;
Instead, he returns undef explicitly:
return undef;
That's a minor mistake, you can get away with it so long as the subroutine is not called in a list context. Unfortunately, that's exactly how the routine is being called. It is expected to return a list of values it has found, the return value being stored in an array. Later, a loop iterates over the contents of the array, producing some final processing and generating output.
The problem with returning undef, rather than merely returning, is that rather than generating an empty list, the code produces an array with one element, 'undef'. So the final loop iterates once, and produces an empty line of output, even though no values were found.
Now that's not a mortal sin, and not something I would flame someone for, if I saw it in a minor script from someone who is trying to improve his Perl. However, in a book claiming to have achieved COOLness, even if not the pinnacle of COOLness, I expect correctness, at the very least.
Other problems include the use of prototypes, the use of C-style loops rather than Perlistic iteration, and more.
Prototypes were a brilliant idea when they came onto the scene, 5 or 6 years ago, unfortunately they create more problems than they solve. Worse, they are totally irrelevant to object-oriented code. Prototypes are best left on the scroll of Perl history as a noble effort that didn't achieve what it set out to. They certainly do not belong in a book, three years or more after the idea was deprecated, being presented as the preferred way to write code.
There are a few situations where C-style loops are usefull in Perl. Most of the time, they indicate a poor understanding of the Perl mindset. While incrementing an index is reasonably efficient C, not quite as efficient as incrementing a pointer, it makes poor use of Perl. Instead of using several operations inefficiently, it is far better to let the language handle the details of iterating over the loop. Not only is it faster, but index variables litter code .. they are part of HOW something is achieved, the implementation, rather than WHAT is achieved. Compare:
for ( my $i = 0; $i <= $#myarray; $i++ ) { # do something with $myarray[$i]; } for my $elem ( @myarray ) { # do something with $elem }
If the goal is to do something with the index numbers, the first example is appropriate. But where the goal is to access or manipulate the elements, the second example is far better ... the implementation is closer to the way we think, making it more self-documenting.
The unfortunate achievement of "Wicked Cool Perl Scripts" is to make me appreciate the few Perl Saints who DO provide an example I aspire to: Randal, Damian, MJD, and the others.
TomDLux
--
TTTATCGGTCGTTATATAGATGTTTGCA
|
|---|