The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

CGI::URI2param - convert parts of an URL to param values

SYNOPSIS

  use CGI::URI2param qw(uri2param);

  uri2param($req,\%regexes);

DESCRIPTION

CGI::URI2param takes a request object (as supplied by CGI.pm or Apache::Request) and a hashref of keywords mapped to regular expressions. It applies all of the regexes to the current URI and adds everything that matched to the 'param' list of the request object.

Why?

With CGI::URI2param you can instead of:

http://somehost.org/db?id=1234&style=fancy

present a nicerlooking URL like this:

http://somehost.org/db/style_fancy/id1234.html

To achieve this, simply do:

 CGI::URI2param::uri2param($r,{
                                style => 'style_(\w+)',
                                id    => 'id(\d+)\.html'
                               });

Now you can access the values like this:

 my $id=$r->param('id');
 my $style=$r->param('style');

uri2param($req,\%regexs)

$req has to be some sort of request object that supports the method param, e.g. the object returned by CGI->new() or by Apache::Request->new().

\%regexs is hash containing the names of the parameters as the keys, and corresponding regular expressions, that will be applied to the URL, as the values.

   %regexs=( 
            id    => 'id(\d+)\.html',
            style => 'st_(fancy|plain)',
            order => 'by_(\w+)' 
           );

You should add some capturing parentheses to the regular expression. If you don't do, all the buzz would be rather useless.

uri2param won't get exported into your namespace by default, so have to either import it explicitly

 use CGI::URI2param qw(uri2param);

or call it with it's full name, like so

 CGI::URI2param::uri2param($r,$regex);

What's the difference to mod_rewrite ?

Basically noting, but you can use CGI::URI2param if you cannot use mod_rewrite (e.g. your not running Apache or are on some ISP that doesn't allow it). If you can use mod_rewrite you maybe should consider using it instead, because it is much more powerfull and possibly faster. See mod_rewrite in the Apache Docs (http://www.apache.org)

INSTALLATION

the standard way:

 perl Makefile.pl
 make
 make install

Currently there are no tests available.

BUGS

I assume, but I did't find any ...

TODO

Implement options (e.g. do specify what part of the URL should be matched)

REQUIRES

A module that supplies some sort of request object is needed, e.g.: Apache::Request, CGI

AUTHOR

Thomas Klausner, domm@zsi.at, http://domm.zsi.at

COPYRIGHT

Apache::FakeEnv is Copyright (c) 2001 Thomas Klausner, ZSI. All rights reserved.

You may use and distribute this module according to the same terms that Perl is distributed under