February 2023

S M T W T F S
   1234
567891011
12131415161718
19202122232425
262728    

Style Credit

Expand Cut Tags

No cut tags
Tuesday, July 23rd, 2002 12:50 pm
I'm getting more motivated at work. I have ONE script that needs debugging... in a language I don't "speak". This is actually fun.

The car place still has my car. Had to skip the gym this morning for lack of car. It @#$ing well better have air conditioning when they're through with it. They replaced nearly every part in the A/C system just weeks ago.

Quote of the day: "WARNING: The consumption of alcohol may leave you wondering what the hell happened to your bra."
Wednesday, July 24th, 2002 04:00 pm (UTC)
$max = [$a=>$b]->[$a<=$b];

This is mildly clever. I'll disect it for the benefit of readers who may not be able to parse it.

Square brackets are an anonymous list constructor in Perl. What results is a scalar value that is a reference to a list. So:

$foo = [1,2];
is the same as:
@foo = (1,2);
$foo = \@foo;

=> is syntactic sugar, and is a synonym for ',' , so what you've done with the first part of the expression is an (anonymous) reference to a list containing $a and $b. It is a cool way to write it because it looks like numeric comparison, but actually does nothing of the sort.

Now, the '->' is the arrow operator used to dereference in Perl. When followed by square brackets, it is expects to dereference a list reference. For instance, with our foo example above, we'd get the first (zeroth, actually) value of @foo with:

$foo->[0];

So... $max = [$a=>$b]->[...];

intends to dereference the anonymous list and access the numerical value indicated by '...'. What is actually contained in that is, this time, an actual numeric comparison is being done with $a<=$b, which if false, will generate 0, the zeroth element, and if it is true, it will be equivalent to 1, giving the other element of the list.

And in more general terms, you are correct... there is more of this in Perl than anywhere else I've ever played, and it is among the multitude of reasons that I love Perl and Perl culture.

If you disliked this bit of fun, you are almost certain to not enjoy the Fun With Perl mailing list, accessible from http://lists.perl.org/ . This list has frequent mailings of things of this sort, along with other things like Perl Golf contests. These last are seeing who can perform a specified task in the fewest number of (key)strokes. You'd be amazed what Perl masters can do in 60 or strokes... I've never seen a problem posed that took more than that. Of course, the winning solution always look like this:

-ni
$_=$#@*(%(#$@#___;eval

so it has little value until you get enough the hang of it to be able to parse those things... but once you have the basics, decoding those and asking questions about them is extremely educational.

I personally suck at these in that I've been programming Perl too long to justify entering those contests in the "beginner" category and yet I can't generally score well even for a beginner, let alone with the main group.

(Anonymous)
Thursday, July 25th, 2002 07:17 am (UTC)
mmm, excessively-clever perl. great for personal hacks, or contests... I think I'd slap anyone who checked that in on my current project :-) (now, if it became an established idiom, that'd be another thing, or if it had a comment, or if there was some reason it was actually *clearer* than the classic version.)
perl has made inroads into the dos space, via ActiveState, especially on the web side of things... but vb/vbscript covered a lot of the "needs more scripting" niche, much to the horror of unix geeks :-)
but yeah, you can always ask for help, even "don't tell anyone I asked, but..." form; you just risk getting long digressions about how some feature evolved from perl 3 :-)

"perl is the yiddish of programming languages"
_Mark_