### Teaching Perl

by Ovid (Cardinal)
 on Apr 05, 2003 at 00:11 UTC 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!

Cheers,
Ovid

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 http://frontiernet.net/~hsweet/programming

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.

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

Create A New User
Node Status?
node history
Node Type: perlmeditation [id://248224]
Approved by silent11
Front-paged by rob_au
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-06-18 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (109 votes). Check out past polls.

Notices?