in reply to Teaching Perl Idioms

It sounds like the first thing you need to do is figure out just what the Perl idioms you're referring to are! For me, the following compose a partial list: Basically, make sure they understand TMTOWDI and that their C-Perl is no better or worse than someone else's Java-Perl or my Perl-Perl. :-)

As for getting them to think in Perl, force them into text-manipulation. That's where Perl started and that's where it shines. Ask them to find all the occurrences of a regex-match in a file. Have them do it in their favorite language, then in Perl. Show them 3-4 different ways in Perl, starting with the most C-like, then moving slowly to Perlisms. Try and remember how you discovered things like:

LINE: while (<INFILE>) { next LINE while /Foo/ .. /Bar/; chomp; # Do stuff here. }
How you discovered the idioms is how you want them to discover them ... through iterative runs of the same solution, but written more and more Perlish.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
Re: Re: Teaching Perl Idioms
by Masem (Monsignor) on Sep 26, 2001 at 21:22 UTC
    As for getting them to think in Perl, force them into text-manipulation. While this is very good advice, I would also urge them to think in terms of list manipulation as well. NOT linked-lists, NOT list-like-arrays, but as mentioned above, the importance of the functions of map, grep, sort, for/foreach, and how one can effectively do loops without a counter, unlike C, C++, or Java. I remember when golf problems started to show up here that many were better solved thinking about using lists and taking advantage of map and friends, than to break it down in any other fashion. Absolutely positively make sure they understand the usefulness of the Schwatzian Transformation as well for sorting complex data items.

    I would almost go as far as introducing elements of functional programming, but that might be too much for a learning class, but it's very important to stress the list aspect of perl over anything else.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

      Now that's a thought! Why not, as bonus problems, introduce a few of the easier golfs? Do something like "Try and solve this under 100 characters. The best I've seen is 53" and have them go at it. Then, explain a few solutions, including the best one. I know that I learned a ton of idioms, especially list-processing, through golfing.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Now I wouldn't go as far as suggesting to teach perl via golf. A good perl golf usually is an end result of knowing where whitespace isn't needed and taking advantage of perl's special variables outside of $_ and @_.

        Mind you, showing a de-obfu golf program (with white space expands and variables named right) and the associated C/C++/Java solution may be useful. Or better yet, show the true power of list processing by comparing the Schwatzian TRansformation vs the equivalent in C. In perl, it's 3 simple lines vs much more than in most other languages.

        -----------------------------------------------------
        Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
        It's not what you know, but knowing how to find it if you don't know that's important