Perl Coding Pleasure
Mar 18
I wrote this piece of code recently. I don’t know why, but I’m terribly proud of it. Nobody here will understand it. That’s OK. I just need to show off for myself sometimes:
opendir(DIR, $backup_directory);
my @sorted_backups = sort {
($m{$a} ||= -M $a) <=>
($m{$b} ||= -M $b) }
map {$rep_directory . "\\" . $_ }
grep { /.ZP$/ } readdir(DIR);
closedir (DIR);
It was originally written as three distinct chunks of code, but I felt like obfuscating and combining things just a bit. And, yes, there are comments explaining this thing in the code, itself. In the end, it makes for a nifty subroutine, too. (A “subroutine” is what you Java folks like to incessantly call “methods.”)
I could also skip the whole “map” line if I just changed directories before that section of code started.
Now, maybe I’ll go work up a full-bore Schartzian Transformation for kicks. . .
Twitter
Email
Mar 18, 2007 @ 14:42:27
Here’s a little function I wrote just now:
if ($blogger==’augie’)
return(nerd);
Mar 18, 2007 @ 19:20:43
Or, in Ruby:
return nerd if blogger == %r{augie}
I doubt that works, but it was fun.
Mar 18, 2007 @ 19:40:30
So that’s a back up of your blog? I don’t read Perl, but I can’t help look when I see code.
Mar 18, 2007 @ 20:29:48
Nah, it’s a process to backup some ZIP files at work. I have a WordPress plug-in to handle backups of the blog. =)
Mar 19, 2007 @ 14:41:44
I understand every line :)
Mar 19, 2007 @ 16:17:37
Liar.
Mar 19, 2007 @ 17:35:23
Man, this just reminds me that I haven’t done any Perl programming in a long time. I’d like to learn Ruby, but without an actual project requiring me to use it, I probably won’t, simply due to lack of free time to learn it otherwise.
Mar 19, 2007 @ 18:16:51
Nice one-liner. Two things I would do a little bit differently might be to use interpolation, i.e. “$rep_directory\\$_”, instead of catenating the pieces together. Or, if you are being very purist, you could use File::Spec, then it becomes catfile($rep_directory, $_)
The other thing is a teensy bug that is not very likely to happen. Your grep will match any file that ends with any character and then ZP, so FOO.AZP is going to match. You probably wanted /\.ZP$/, perhaps?
If you want to be a super-Perl obfuscator, you’d put the opendir, readdir, and closedir in a do block inside the sort and map
my @sorted_dirs = sort { … } map { … } do { opendir(); @_ = readdir(); closedir(); @_; };
Thanks for the Idol updates, Augie!
Mar 20, 2007 @ 09:01:47
Phil – That’s a big part of learning any language. I have a couple of ideas for things with Ruby on Rails that I’m working on right now. One is painfully simple — it’s almost more work to do the CSS than it is the coding. But I’ve still learned a lot already. The next thing I want to work on will introduce some higher level Rails and Ruby stuff. In the meantime, I can always work on porting my Perl scripts at work into Ruby to help learn the language.
David — Thanks. You’re absolutely right. There’s a missing “\” before that period. I just looked at the original code and ran a couple of test cases, and I have it right in there. I factored out the file extension I was looking for and made it a parameter to the subroutine. Now, when I pass “.ZP” as that parameter and grep for it, it will get .ZP files but not .AZP files.
It’s something I should have caught, though, when I simplified it to post here. Good catch!
As for concatenation: I don’t know why I did it the way I did. It would be simpler with interpolation. I think it’s just a weird quirk of coding style I’ve developed over the years. I lay everything out into separate pieces rather than jamming it all in together. Of course, given what this code looks like, jamming it all together might have been more appropriate.
And now that I think of it – I remember reading in a Perl book somewhere (”Intermediate Perl,” perhaps?) that using a comma instead of a period in your concatenations is slightly quicker in large cases. I need to get back into that habit.
But, in the end, I love your one-liner. That’s even more daring than I’d attempt. Thanks for the tips!
Mar 20, 2007 @ 09:12:14
There’s also an “i” at the end of that REGEX, just in case there’s anything wonky going on with the capitalization issues of filenames.