The learning curve is steep. It’s hard to know what to prioritize. Perl makes life easier, though, in that the bottom 30% of the language is so all around useful, you can get all kinds of things done even though you “speak Perl like a three year old.” For the rest of it, I just stop every few weeks and take an hour to focus on the two or three things that bug me the most. My theory has been, that as long as the combination of Unix-like shell, Emacs editor, and Perl scripting is applied to my daily work, there’ll always be enough payoff that it’s worth my while to learn the things I’ve been putting off– so that in 6 to 8 months I’ll actually start to gain some genuine skill.

Here’s a few things like that that I finally took the time to address:

(global-font-lock-mode 1)

Put this in your .emacs file to enjoy the wonders of syntax highlighting. Yea. (I wondered why the Windows version was in color where the Cygwin version wasn’t….)

There’s also two Perl modes, for some reason. I actually kind of liked the default one better than the M-x cperl-mode that you’re supposed to use instead. In color, your hashes look atrocious… and your useless spaces show of as abrasive underscore lines…. I use those lines to mark my place in my project– to sort of delineate where I’m working. Cperl-mode seems a little more sluggish to me when it has to place your braces where they belong, but I like the way it spaces things better. (And people talk about how hard it is to parse Perl… it wasn’t long before I broke the syntax highlighting with a line that had a mess of single and double quotes on it. Maybe switching color mode on was not a good idea.)

Perl has anonymous functions and also functions that operate in list context. As much as Larry hates parentheses, it’s clear that he doesn’t hate Lisp concepts….

If you’ve got an array of strings, you can grep them with an anonymous function. Map can be used in a similar way:

my @array = qw/ apple bus cat dog elephant/; my @things = grep {length($_) > 3} @array; print "*$_*

" for @things; my @stuff = map {"--$_--"} @things; print "test: $_

" for @stuff; # Altogether now: print map {"!!!$_!!!

"} grep {length($_) == 3} @array;

Now here’s the cool thing. I’m thinking to myself… wouldn’t it be great if you could refer to files streams in list context? And sure enough…

print map {“!!!$_!!!

”} grep {length($_) == 3} <>;

This works! All of that accidental complexity in my Perl scripts due to excessive looping and if-then-else blocks… this one idea puts a huge dent in it.

I have to admit, this gave me flashbacks to my college Calculus class. We’d been working through a huge number of problem sets for a few weeks… and the professor comes in and teaches us a trick that showed us we were really doing things the hard way. The “regular” kids were disgusted. Why did he waste all our time? My theory was, that for most of us, we would not have appreciated the trick (and maybe not even understood it) if we hadn’t done the work first. Back at the code bench, this translates to… write bad Perl scripts to do practical things at work. Then clean them up with map and grep. Now you *really* know what they were trying to tell you back in chapter 3 of SICP! You know it down in your fingernails….

Okay, one last note. A few weeks ago, we saw that to really get functions to work with accepting arrays as arguments and returning groups of them for their return values, you had make the mental and semantic leap to begin thinking in terms of references. This seems to be a little tricky, because it doesn’t appear to be a “reference” like what I’ve seen in other languages– maybe it is, but if I “my” the sucker in a subroutine, it looks like that’s copying it, at the very least. Anyways, I’ll gradually assimilate that in time. We don’t really care what a reference really is yet– we just want stuff to work! Especially with hashes!!

my ($foo, $bar) = test2(); my $value = ${$bar}{2}; print "The value of foo is $foo and the value is $value

"; print "We could have just said, '${$bar}{2}', too.

"; sub test2 { my $foo = "hello"; my %bar; $bar{2} = "world!"; return ($foo, \%bar); }

So, to cast the reference (stored in a scalar) into a hash so that we can ‘talk’ to it, we have to ‘fancy’ it in two places. I was thinking that the curly braces around the key would tip Perl off as to what we were trying to do…. And when that didn’t work, I was thinking that some sort of casting with a % sign somewhere would be the ticket. Thanks to Intermediate Perl, though, we know what we need to keep rolling….

Advertisements

Like this: Like Loading... Related