You've seen some of these posts from me before. Somehow, I manage to come across some of the most interesting Perl code. Here are a couple of gems I plucked from an old program that I found on one of our boxes.

First, for all of you who detest using push:

my %hash = ( foo => 1, bar => 2, baz => 3 ); my @result; foreach ( keys %hash ) { $result[ @result ] = "$_:$hash{ $_ }"; }

Interestingly, it doesn't fair too poorly in a (admittedly hasty) benchmark:

my %hash = ( 1 .. 100000 ); timethese( 500000, { 'obfu' => 'my @result; for my $key ( keys %hash ) { $result[ @result ] = "$key:$hash{ $key }"; }', 'push' => 'my @result; for my $key ( keys %hash ) { push @result, "$key:$hash{ $key }"; }', }); #Results: #Benchmark: timing 500000 iterations of obfu, push... # obfu: 5 wallclock secs ( 2.93 usr + 0.01 sys = 2.94 CPU) @ 1 +69836.96/s (n=500000) # push: 3 wallclock secs ( 2.94 usr + 0.00 sys = 2.94 CPU) @ 1 +70357.75/s (n=500000)

And here's an interesting attempt to validate arguments to an object constructor. What the heck was this programmer thinking?

sub new { my ($class, $orderID, $owner, $limit) = @_; foreach (qw'orderID') { (croak __PACKAGE__ . "->new() needs a ${_} +.") unless (eval("\$$_")) } # rest of constuctor ... }

Any accidental obfuscations you'd care to share?

Cheers,
Ovid

Vote for paco!

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
(MeowChow) Re: Accidental Obfuscations
by MeowChow (Vicar) on Jul 27, 2001 at 03:11 UTC
    I have, on rare occasion, found use for the `$list[@list] = ...' syntax, where push would not make sense.

    More idiomatically, the first snippet should be reduced to the following:
      
    my @result = map "$_:$hash{ $_ }", keys %hash
    I have a hunch that the coder didn't really want an array sorted in the arbitrary order that keys happens to return, and that this is actually part of a larger body of nonsense :)

    The second snippet is an absolute abomination of sanity checking code, which is exactly the sort of thing I was warning you about here :-p

    update: Whoops, just noticed that maverick already pointed out the map idiomatization.

       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: Accidental Obfuscations
by maverick (Curate) on Jul 27, 2001 at 02:45 UTC
    <sarcasm>
    How about the horridly unreadable map?
    my @result = map { "$_:$hash{$_}" } keys %hash;
    </sarcasm>

    /\/\averick

Re: Accidental Obfuscations
by bwana147 (Pilgrim) on Jul 27, 2001 at 16:33 UTC

    I must say something for the defense of the poor coder that's being shamelessly laughed at here :-)

    I think that the second snippet is intended as a generic parameter checker. The author must copy and paste that line and simply modify the list of mandatory parameters in the qw//.

    I'm not saying whether this is Good or Bad, but you asked what the heck was this programmer thinking...

    --bwana147