I was hacking on Net::DNS today (I should get back to hacking on Dyn!), and I wrote this:
if ($string =~ m/^
([a-zA-Z0-9]{2}) # AFI ($1)
([a-zA-Z0-9]{4}) # IDI ($2)
([a-zA-Z0-9]{2}) # DFI ($3)
([a-zA-Z0-9]{6}) # AA ($4)
([a-zA-Z0-9]{4}) # Rsvd ($5)
([a-zA-Z0-9]{4}) # RD ($6)
([a-zA-Z0-9]{4}) # Area ($7)
([a-zA-Z0-9]{12}) # ID ($8)
([a-zA-Z0-9]{2}) # Sel ($9)
/x)
{
$self->{'afi'} = $1;
$self->{'idi'} = $2;
$self->{'dfi'} = $3;
$self->{'aa'} = $4;
$self->{'rsvd'} = $5;
$self->{'rd'} = $6;
$self->{'area'} = $7;
$self->{'id'} = $8;
$self->{'sel'} = $9;
}
Something deep down tells me I should be using unpack(), but I know and love regexen. Perhaps tomorrow I will get to know pack/unpack better.
Example (Score:2)
Also, why [a-zA-Z0-9] instead of just \w?
Re:Example (Score:2, Insightful)
Maybe he's worried about underscores. Maybe he's even using locale.
Try a hash slice too: (Score:3, Informative)
Reply to This
Re:Try a hash slice too: (Score:3, Informative)
Benchmark: timing 1000000 iterations of regex, unpak...
regex: 40 wallclock secs (38.95 usr + 0.00 sys = 38.95 CPU) @ 25673.94/s (n=1000000)
unpak: 2 wallclock secs ( 2.61 usr + 0.00 sys = 2.61 CPU) @ 383141.76/s (n=1000000)
I hit a 'lameness filter' or I would have posted the code as well but you should be able to replicate the results with little effort. BTW, this was on a Sunbl
Re:Try a hash slice too: (Score:2)
Re:Try a hash slice too: (Score:2, Funny)
--
"Cake or Death!"