The Road To Better Perl Programming: Chapter 4 9
Frank writes: "This series of articles on developerWorks comprises a complete guide to better programming in Perl. In this fourth installment, Teodor introduces functional programming and several essential Perl idioms important for Perl programmers looking for speed and elegance in their code, such as the map() and grep() functions, and the Schwartzian and Guttman-Rosler transforms."
Learning perl (Score:1)
Kind of weak (Score:2, Informative)
It bugged me when he used map() in a void context (listing 3). It's even in the Perl FAQ that it's frowned upon.
I would call the article "idiomatic perl" instead.
Re:Kind of weak (Score:4, Informative)
Perhaps it is, but the primary reason it is frowned upn is because map in a void context returned a list, so it was inefficient. That behavior's been changed; map in a void context is no longer a technical problem. Now it is merely a stylistic one.
Re:Kind of weak (Score:1)
I wasn't too keen on this text, either. Early on he states how procedural programming differs in that functions are used for their side effects where as functional programming uses functions for the values returned. Then he turns around does a print inside of a map block - using functional constructs to do side-effects? bleh.
I much prefer the approach taken in Randal Schwartz and Joseph Hall's Effective Perl Programming... which has a wonderful "idiomatic perl" chapter, I must add =)
-Ducky
Re:Kind of weak (Score:1)
All the examples are meant for beginner programmers. I hope experts will forgive the simplicity, but I can't go into high-level constructs in a beginner article.
The particular one you mention (listing 2) compared a foreach() loop with a map() that behaved identically. The point was to demonstrate, to a novice, how to use map() in a familiar context.
I think you are right, I should have done
print map { "$_ => $ENV{$_}\n" } sort keys %ENV;
that would be a "pure" FP approach. I'll make sure to make the change in future versions, but I don't think it's major enough to warrant modifying the current version on the developerWorks site.
If you have suggestions, you can always contact me and let me know. I value reader opinion very highly.
Thanks
Ted Zlatanov
Re:Kind of weak (Score:1)
This is chapter 4 in a book. The book is (partly) about introductory Perl programming. The chapter is on Functional Programming.
listing 3 illustrates how map() aliases $_ to each element of the list passed to it. The point is that without anything extra done, just calling map() in a void context, you are making changes to the original list. Sorry if that was unclear.
Thanks
Ted Zlatanov
a light dawns... (Score:2, Interesting)