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


In reply to 'Wicked Cool Shell Scripts' is a _don't_buy_ by TomDLux
in thread Wicked Cool Shell Scripts implemented in Perl by szabgab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.