perl evil for the week

Al Tobey albert.tobey at priority-health.com
Tue Jul 15 09:55:00 CDT 2003


That's really cool - I'm not sure why I didn't come across that.  The
only thing I don't really like is, when in strict mode, you have to
pre-declare %combined as a hash.  Yours is easier to read, though.  It's
just not evil enough - I guess I should shave my goatee and drop the
maniacal laugh after all....

use strict;
my %combined = ();
@combined{@foo} = (@bar);

This one:
use strict;
my @combined{@foo} = (@bar);

Throws an error at compile time.  Still good stuff :) TMTOWTDI!
-Al

On Mon, 2003-07-14 at 20:26, Matt Diephouse wrote:
> Rather than use map to create the hash, you could just use a hash slice.
> 
> #!/usr/bin/perl
> 
> @foo = qw( a b c d e f g );
> @bar = qw( h i j k l m n );
> 
> # here is the hash slice
> # notice that it uses @ as the sigil
> # but also uses {}'s
> @combined{@bar} = (@foo);
> 
> foreach my $key ( keys(%combined) ) {
>      print "$key => $combined{$key}\n";
> }
> 
> For more information, go to your terminal and type `perldoc perldata`.
> 
> matt
> 
> On Monday, July 14, 2003, at 01:13  PM, Al Tobey wrote:
> 
> > First off, my little example program shows some new behavior that
> > everybody should be aware of: perl 5.8.1+ will explicitly randomize the
> > order of your hash keys.  This is supposed to fix some possible DOS
> > attacks against perl CGI scripts that guess hash key orders.
> >
> > Taking two arrays with the same number of elements and putting them 
> > into
> > a hash in one line.  Inspiration credit goes to Damian Conway during 
> > his
> > Advanced Object Oriented Perl class at OSCON.  He used map a number of
> > times, each time saying something like, "this is evil, so don't do it
> > unless you really know what you're doing - and please comment it."
> >
> > #!/usr/bin/perl
> >
> > @foo = qw( a b c d e f g );
> > @bar = qw( h i j k l m n );
> >
> > # here is the evil
> > %combined = map { $bar[$_] => $foo[$_] } 0..$#foo;
> >
> > foreach my $key ( keys(%combined) ) {
> >     print "$key => $combined{$key}\n";
> > }
> >
> > -------------------------------
> > tobeya >perl /tmp/test.pl
> > l => e
> > n => g
> > k => d
> > h => a
> > m => f
> > j => c
> > i => b
> >
> > This was really useful because I was parsing the output of a unix tool
> > which had known field names, such as vmstat.  I hard-coded the field
> > names, then mapped the output of the command against the field name
> > array into a hash.  Very useful in my line of work.
> >
> > -Al Tobey
> >
> > ---------------------------------------------------------------
> > P.S. Here is a more complex but possibly better example:
> >
> > # untested, so probably has errors
> > @header = qw( proc_r proc_b proc_w swpd free buff cache
> >            swap_in swap_out blocks_in blocks_out interrupts
> >            context_switches user_cpu system_cpu idle_cpu );
> >
> > # run vmstat 10 times with 5 seconds between each listing
> > open( VMSTAT, "/usr/bin/vmstat -n 5 10 |" )
> > 	|| die "could not execute vmstat: $!";
> > <VMSTAT>; <VMSTAT>; # skip the headers
> >
> > my @data = ();
> > while ( my $vmstat = <VMSTAT> ) {
> >    my @splitstat = split( /\s+/, $vmstat, $#header+1 );
> >    my %combined = map { $header[$_] => $splitstat[$_] } 0..$#header;
> >    push( @data, \%combined );
> > }
> > close( VMSTAT );
> >
> > # do something with @data ...
> >
> >
> >
> >
> >
> > ** ** **  PRIVILEGED AND CONFIDENTIAL  ** ** **
> > This email transmission contains privileged and confidential 
> > information
> > intended only for the use of the individual or entity named above.  Any
> > unauthorized review, use, disclosure or distribution is prohibited and
> > may be a violation of law.  If you are not the intended recipient or a
> > person responsible for delivering this message to an intended 
> > recipient,
> > please delete the email and immediately notify the sender via the email
> > return address or mailto:postmaster at priority-health.com.  Thank you.
> 




More information about the grand-rapids-pm-list mailing list