Please create an account to participate in the Slashdot moderation system

 



Forgot your password?
typodupeerror
×
Perl Programming

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."
This discussion has been archived. No new comments can be posted.

The Road To Better Perl Programming: Chapter 4

Comments Filter:
  • Is an curve which quickly crosses the useful threshold very quickly but goes asymptotic to guruness.
  • Kind of weak (Score:2, Informative)

    I wasn't sure if he teaching "intro to perl" or "intro to functional programming".

    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)

      by pudge ( 3605 ) <<slashdot> <at> <pudge.net>> on Sunday January 20, 2002 @09:18AM (#2871870) Homepage Journal
      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.

      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.

    • 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

      • (disclaimer: I'm the author of the article)

        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
    • (disclaimer: I'm the author)

      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)

    by sohp ( 22984 )
    Nice nice article. Despite having used Perl for a number of years and become quite proficient in most parts of the language, I never really "got" the use of map and grep. Zlatanov's two central tips, "read from right to left" and "it's like foreach without an explicit looping condition" finally brought the principles home to me. I can finally read and understand The Schwartzian Transform!

Every program is a part of some other program, and rarely fits.

Working...