>>>>> "Marcin" == Marcin Kasperski <[EMAIL PROTECTED]> writes:

Marcin> Maybe it could be of some interest where I happened to get this
Marcin> problem. I got it in my custom TransHandler implemented to reduce stat
Marcin> calls and to obtain 'select from multiple directories' effect.

Marcin> ... (config file) ....

Marcin> our %STATIC_FILES = (
Marcin>     '/img' = [ '/alternative/img', '/myapp/install/img' ],
Marcin>     '/css' = [ '/alternative/css', '/myapp/install/css' ],
Marcin>     ...
Marcin> )

Marcin> ... (transhandler file, simplified of course) ...

Marcin> sub handler {
Marcin>     my $r = shift;
Marcin>     my $uri = $r->uri;
Marcin>     ... detecting dynamic handlers ...
Marcin>     while( my($url, $dirs) = each %STATIC_FILES ) {
Marcin>         if( $uri =~ m{$url/(.*)$} ) {
Marcin>             foreach my $d (@$dirs) {
Marcin>                 my $file = "$d/$1";
Marcin>                 if( -f $file ) {
Marcin>                    $r->filename($file);
Marcin>                    return OK;
Marcin>                 }
Marcin>             }
Marcin>         }
Marcin>     }
Marcin> }

That's actually the wrong data structure then.  What you want
if you're only ever accessing it as a list, is a list!

And, you're needlessly recompiling the regex each time.  Here's
a much better way to do that...

    our @STATIC_FILES = (
      [ qr{^/img/(.*)$} => [ qw(/alternative/img /myapp/install/img) ],
      [ qr{^/css/(.*)$} => [ qw(/alternative/css /myapp/install/css) ],
      ...
    );

    sub handler {
      my $r = shift;
      my $uri = $r->uri;
      for (@STATIC_FILES) {
        my ($pat, @dirs) = @$_;
        if ($uri =~ $pat) {
          my $tail = $1;
          foreach my $dir (@dirs) {
            my $file = "$dir/$tail";
            if (-f $file) {
              $r->filename($file);
              return OK;
            }
          }
        }
      }
      return DECLINED;
    }

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Reply via email to