You can get anything you want ...

Matthew R. Heusser HEUSSERM at student.gvsu.edu
Mon Jul 14 14:46:15 CDT 2003


I got to here Alison Randal talk about Perl 6,
all to the tune of "Alice's Resturant"

http://oscon.kwiki.org/index.cgi?AllisonsRestaurant

Dude.  Awesome.

Gr.pm should consider a kwiki.

regards,

Matt H.
-----Original Message-----
From: Al Tobey <albert.tobey at priority-health.com>
To: grand-rapids-pm-list at happyfunball.pm.org
Date: 14 Jul 2003 13:13:27 -0400
Subject: perl evil for the week

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