perlquestion
graq
<p>
I have the requirement that I need to map the start of (what is effectively) PATH_INFO to an int (PK on some table). I had a look at <a href="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritemap">Apache RewriteMap</a> and chose to go down that route. I'm not sure what go-faster techniques are valid in this environment, or whether they would have any impact (e.g. [cpan://memoize]).
</p>
<readmore>
<p>Using files to lookup the values, RewriteMap only handles simple string comparisons, which does not cater for my need.</p>
These are my test URLs:
<code>
http://myurl.com/aa/index.html
http://myurl.com/aa/bb/index.html
http://myurl.com/aa/foo/index.html
http://myurl.com/cc/index.html
http://myurl.com/cc/dd/index.html
</code>
This is my mapfile:
<code>
aa 1
bb 2
cc/dd 3
cc/ee 4
</code>
Consider using the following mapping:
<code>
RewriteMap mapping txt:mapfile.txt
RewriteRule ^/(.*)/ - [E=TEST_VAL:$1,E=TEST_RESULT:${mapping:$1},NS]
</code>
<p>Because Apache uses a simple text match, the '/'s (slashes) in the URLs break the mapping. Which led me to use a program.</p>
Apache Config: <code>RewriteMap mapping prg:/my/dir/mapper.pl</code><br>
<p>mapper.pl:
<code>#!/usr/bin/perl
my $CONFIG_FILE = 'mapfile.txt';
my $config = &get_config;
$| = 1;
while(<STDIN>){
my $pk = lookup($_);
print "$pk\n";
}
sub get_config
{
my $apache_boot_time_message = "$0: Setting up Apache RewriteMap [$CONFIG_FILE]";
print STDERR "$apache_boot_time_message\n";
my %config;
open( CONFIG, '<', $CONFIG_FILE ) or die ("Could not open $CONFIG_FILE: $!");
while(<CONFIG>){
chomp;
my $line = $_;
my @line = split('\s+', $line);
$config{$line[0]} = $line[1] unless $line =~ /^#/;
}
close CONFIG;
return \%config;
}
sub lookup
{
chomp;
my $DIR = shift;
my @parts = split( '/', $DIR );
for( 0..$#parts ){
my $dir = join('/', @parts[0..$_]);
return $config->{$dir} if $config->{$dir};
}
return "NULL";
}
</code>
<p>As this script is going to be hit so many times by the webserver, I wondered if anyone had ideas on speeding it up?</p>
</readmore>
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-79379">
<p>-=( <a href="http://www.graq.co.uk">Graq</a> )=-
</div></div>