in reply to Perl aesthetics: the good, the bad, the ugly.

I'm glad you picked argument passing as your example, since the ugliest piece of bad Perl I've ever seen(Hey, two out of three ain't bad) is argument passing.

I was beginning to maintain an out-of-house piece of Perl, and was happily reading through it sorting the small pile of wheat from the large pile of chaff when suddenly I hit a brick.

The programmer had, I think, intended to get the kind of default arguments that hashes can easily provide.. but had tried to do it without any kind of hash. They also had odd argument ordering, with related arguments randomly places around the sequence so often the value you needed to default would be in the middle of the 'full' list. I think they were just adding new items to the end as they became required.

I'm now in a new job, so I can't get this code to show you, but here's the essence of how it worked. I'm refusing to comment this, for those Initiates of Perl who can't work out how this works think yourselves lucky and go and reread the hash method.

And yes, the 'use strict' and -w bits were added by me for my example. They didn't use these either, oddly enough.

#!/usr/bin/perl -w use strict; DoStuff (1,2,3); DoStuff (1,3); DoStuff (2); sub DoStuff { my ($a, $b, $c) = (-1, -2, -3); if ($#_ == 2) { ($a, $b, $c) = @_; } elsif ($#_ == 1) { ($a, $c) = @_; } elsif ($#_ == 0) { ($b) = @_; }; print "A= $a\n"; print "B= $b\n"; print "C= $c\n"; }

I'm thankful not to be in that job anymore.

Other bits of wonder I've seen..

I love maintaining the code of people who don't really know Perl, it's ingenious some of the things they can get up to.

Replies are listed 'Best First'.
Re: Re: Perl aesthetics: the good, the bad, the ugly.
by agentv (Friar) on May 01, 2002 at 20:16 UTC
    ...but this is proof not of the ugliness of Perl, but of the ugliness of someone's mind.

    Where that person wrote:

    sub DoStuff { my ($a, $b, $c) = (-1, -2, -3); if ($#_ == 2) { ($a, $b, $c) = @_; } elsif ($#_ == 1) { ($a, $c) = @_; } elsif ($#_ == 0) { ($b) = @_; }; print "A= $a\n"; print "B= $b\n"; print "C= $c\n"; }

    We might instead write:

    sub DoStuff { my ($a, $b, $c) = @_; $a = defined $a ? $a : -1; $b = defined $b ? $b : -2; $c = defined $c ? $c : -3; print "A= $a\n"; print "B= $b\n"; print "C= $c\n"; }

    And while this lacks some elegance (for instance, I might want to create a loop that simply tests $_[0] and then shifts the top value into the next of my variables), I think it would be a simple improvement.

    The real point is that the language allows us to be ugly. Some of choose to do so.

    ---v

      Note that if there are 2 arguments, the original code set $a and $c, not $a and $b.

      -Mike