Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Teaching Perl

by Ovid (Cardinal)
on Apr 05, 2003 at 00:11 UTC ( #248224=perlmeditation: print w/replies, xml ) Need Help??

While many people have asked questions about how to teach Perl or where to learn it, an email today on the Perl Advocacy list mentioned a college professor that was not only teaching Perl, but was surprised by the overwhelming demand for it. You read about the course, if you're curious. In just browsing through it, I noticed a few things that were a bit odd, but all in all, it seemed far better than most material out there. Here's one of the questions from a quiz:

# Given: sub pair { my @out = (); for (my $i=0; $i<@_; $i+=2) { push(@out,[$_[$i],$_[$i+1]]); } return @out; } # What is the value of the expression map { &{$_->[1]}($_->[2]) ? $_->[0] : () } map { [$_->[0],$_->[1],'meowmoo'] } &pair ( 'cat' => sub { $_[0] =~ /meow/ }, 'dog' => sub { $_[0] =~ /arf/ }, 'cow' => sub { $_[0] =~ /moo/ } ) # in an array context?

Reading through the course information is quite interesting. Enjoy!


New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)

Replies are listed 'Best First'.
Re: Teaching Perl
by hardburn (Abbot) on Apr 05, 2003 at 23:22 UTC

    Break it up peice by peice.

    pair takes a "hash" and creates an AoA. Consider this code:

    my @got = pair( . . . );

    Where $i is any integer between 0 and scalar(@got), $got[$i][0] will contain the "hash" key, and $got[$i][1] will contain its value.

    Taking the map statements in order of execution, the first one is:

    my @got2 = map { [$_->[0],$_->[1],'meowmoo'] } @got;

    @got2 is also an AoA, but this time containing three elements. The first two are exactly as they are in @got, and the thrid one is always 'meowmoo'.

    The second map statement is a bit trickier:

    my @got3 = map { &{$_->[1]}($_->[2]) ? $_->[0] : () } @got2;

    This one needs to be broken up further:

    &{$_->[1]} # Get the subroutine referanced in $got2[$i][1] ($_->[2]) ? # Pass in the value of $got2[$i][2] ('meowmoo') $_->[0] : # If the subroutine returned true, then # pass back the key of the "hash" associated # with that subroutine. () # Otherwise, return undef

    @got3 is a simple array, and will contain ('cat', undef, 'cow'). Actually, I'd have to run the code to check if that undef is really there. Hopefully, this is a multiple-choice test and answers with and without the undef are not both present :) If I had to choose, I'd say that it would be.

    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      I think you're 99.5% correct. A () is not the same as undef. Take a look...

      bash$ perl -e 'print join(",", ('meow', (), 'moo')), "\n";' meow,moo

      But the rest of the analysis is perfect, so a big ++ for you :)

      Best regards

      -lem, but some call me fokat

Re: Teaching Perl
by hsweet (Pilgrim) on Apr 05, 2003 at 22:07 UTC

    I just looked over (quickly) the profs site. Dese college guys are smart! Much harder that I do. I'm in the middle of my second time teaching my perl course. High school level to kids with no experience in programming anything more involved than a VCR. I'm happy when they can write a working counter after 10 weeks. (And they can) I've got my course materials up at

    Time flies like an arrow, fruit flies like banannas

Re: Teaching Perl
by tbone1 (Monsignor) on Apr 08, 2003 at 12:53 UTC
    Interesting ...

    I've been out of academia for far longer than I'd car to admit, so it is always a bit jarring to be reminded of what a professor thinks is important to know compared to some schlub (like me) who just needs to get something done on a deadline. Still, this looks like another good resource, and I might have to look through it myself to see what I can pick up. If nothing else, it will be good practice for technique and analysis.

    Ain't enough 'O's in 'stoopid' to describe that guy.
    - Dave "the King" Wilson

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://248224]
Approved by silent11
Front-paged by rob_au
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-05-20 04:55 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (72 votes). Check out past polls.