I found myself responding, earlier today, to a request for a list of reasons for using open document formats (such as, conveniently, the OpenDocument Format) instead of closed, proprietary formats. This was in a non-programming discussion forum, in a thread about things unrelated to programming except in the loosest sense in that office application design came up briefly, and the question to which I responded was distinctly unrelated to programming.
My direct response to this involved posting an ad-hoc Perl script that stored the bullet points of my answer in an array. The script, if run, would randomly (meaning by use of srand and rand()) select one of the points and print it to STDOUT.
Have I been spending too much time thinking about Perl lately? Can you, the Monks, provide me with some more symptoms I should be looking for that I'm afflicted by Too Much Perl?
(I duplicated the code in this weblog entry, which collects most of my commentary in the discussion thread in question in one cohesive essay, in case you're interested in seeing it. It's not remarkable enough to bother reposting here, though. Waste of screen real estate, really.)
|
- apotheon
CopyWrite Chad Perrin |
if you did one of these lately :
Then you definetely Perl too much :)
Oh, come on now. That would never work. If $red was true, you'd get all your tshirts, regardless of colour, while, if false, you'd be overdressed at the beach.
What I think you meant was: grep { $_->{colour} eq 'red' } @tshirts or, if you're an OO type of guy, grep { $_->colour() eq 'red' } @tshirts.
However, even that can get a bit messy. I mean, some of us have two, three, or even more, red shirts. Putting them all on at once would look silly, even for a computer geek. (Though, if you're in a workplace filled with perl geeks, you could do that one day - show up wearing all your red shirts, and if anyone asks why you're wearing so many shirts, just tell 'em that you did a grep { $_->has_colour('red') } @tshirts on your closet that morning...). Instead, what you really want is either use List::Utils qw/first/; first { $_->colour() eq 'red' } @tshirts or possibly my @reds = grep { $_->colour() eq 'red' }; $reds[rand @reds] depending on whether you want to be efficient or you prefer some variety.
Me? Too much perl? Nah ...
sort and grep are called in void context; it's just frantic early morning thrashing around, before the brain kicks in.
Presumably, the code later on will end up with the user dressed. Or not, depending on the sort of day it's been.
I usually thrash about in the morning amongst the $black shirts, actually. Also, the $nerd shirts (my Perl Camel shirt, my Perl obfu shirt, my "talk nerdy to me" shirt, my MC Escher shirts, and so on).
I'm not sure I actually have any $red shirts. Thank goodness: I don't want to be the one guy that never returns from the away team.
|
- apotheon
CopyWrite Chad Perrin |
"Never"? This is Perl!
{
package Tie::ColorTest;
sub TIESCALAR
{
my( $pkg, $color ) = @_;
bless \$color, $pkg
}
sub FETCH
{
my $self = shift;
$_->{'color'} eq $$self
}
}
tie my $red, 'Tie::ColorTest' => 'red';
Oh, a red tie. Does it have flashing lights and play a tune?
I guess it could. If it's a red tie, that makes it a power tie, which means it would certainly be capable of running lights and sound.
|
- apotheon
CopyWrite Chad Perrin |
If you look at the following code and immediately understand it, then you have been spending way too much time with Perl. I suggest you take a few doses of Java. :-)
perl -p -e '$\+=$_}{'
The -p switch instructs perl to wrap your code with:
LINE: while (defined($_ =)) { # Your code here } continue { die "-p destination: $!\n" unless print $_; }
This transformation is so direct that you can use the token }{ to your advantage. My original code now becomes:
LINE: while (defined($_ =)) { $\ += $_; } { } continue { die "-p destination: $!\n" unless print $_; }
The while loop now focuses on summing the input (converting each line to a number in Perl's fashion, i.e. stopping after any non-number chars). The continue is now attached to an empty run-once block. So, the printing of $_ only occurs once, and after the input has been exhausted, and $_ is now undef. The print statement prints the empty $_, but also always prints $\ (the line terminator), which currently has our sum.
So, this is a sum command. Can be used with wc ... | perl ... or du ... |, etc.
I never code like this in production code, of course! ;-)
Ted Young
($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)I guess I'm in the clear, then. I understood what the code did before reading your explanation, but not "immediately", so I should be safe to spend more time with Perl. Whew. What a relief.
It was the numerical context that hung me up for a few seconds. I kept wondering what good a += operator was to $\ since, like a good little code monkey, I would (almost) never want to add numbers to it from a file for no purpose other than printing them out, and would thus not have fed it numerical input, which didn't make sense to me. Silly me, I was thinking "useful code", not "mild obfu".
|
- apotheon
CopyWrite Chad Perrin |
Seems all very sensible to me, apotheon, no reason to worry, do carry on!
Perl certainly is not like a drug you could ever get too much of =)
Cheerio, Sören
:-)
Good point. Maybe I should ask my non-PerlMonk friends.
Then again, my non-PerlMonk friends would probably consider any amount of use of Perl (by choice) to be insane, so I'm not sure they're a very good source of judgment either. Er. Unless that just indicates something profound about the use of Perl and its relation to insanity, of course.
|
- apotheon
CopyWrite Chad Perrin |
I find myself wanting to write return if ... in C++ fairly often these days. Is that a symptom?
Not Perl, but PerlMonks OD leads me to look for the ++ and -- buttons on other web sites, or sometimes even emails! The fact that other (non-programmer) members of the household have come to use ++ from time to time may be another indication.
Oh, yeah, I do that all the time — think about how to apply ++ and -- operators, or += (though never -= oddly enough), in all kinds of non-Perl contexts. Another one: I often want to use a while loop and regex (or, alternatively, the little Find box in Firefox) to find something I just read a few pages ago when I'm reading a book.
|
- apotheon
CopyWrite Chad Perrin |
perlmonks.org content © perlmonks.org and Anonymous Monk, apotheon, bluto, GrandFather, Happy-the-monk, jdporter, Tanktalus, TedYoung, wazoox
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03