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 | |
|
Re: Accidental Obfuscations
by maverick (Curate) on Jul 27, 2001 at 02:45 UTC | |
|
Re: Accidental Obfuscations
by bwana147 (Pilgrim) on Jul 27, 2001 at 16:33 UTC |