[cgiapp] CGI::Application::Dispatch implementation

Michael Peters mpeters at plusthree.com
Fri Feb 15 17:00:40 EST 2008


Bruce McKenzie wrote:
> CGI::Application::Dispatch is a favorite plugin of many on this list,
> and it used to be one of mine, but it stopped working in my setup at
> version 2.x. Never figured out why, and I'm OK doing things the
> old-fashioned way, but I thought I'd give it another shot.
> 
> Suppose I have:
> 
>   directory cgi-bin/admin containing
>     a webapp called "test.pl"
>     a directory Modules,

Btw, this is normally called "lib" and not "Modules" for most Perl projects.

>           which contains Notify.pm
>                which has a run mode "test"
> 
> I want to call the following under normal CGI
> http://localhost/cgi-bin/admin/test.pl/notify/test
> 
> What is the simplest way to write the dispatch app? As I understand the
> documentation, this should do the right thing:
> 
>    #!/usr/bin/perl
>    use strict;
>    use FindBin qw($Bin);
>    use lib "$Bin/Modules"; # add "Modules" dir to @INC just in case   
>    use CGI::Application::Dispatch();
> 
>    CGI::Application::Dispatch->dispatch(prefix=>"Modules");

Is your module's package called "Modules::Notify" or just "Notify"? If it's the
first then change your "use lib" line to:

  use lib $Bin;

If it's the latter, then you don't need a prefix at all:

  CGI::Application::Dispatch->dispatch();

> But, obviously, I don't understand the documentation, because, this, and
> every other attempt at it produces only a "Not Found" error.

You can turn on debugging to see how things are getting translated by putting
debug => 1 into your call to dispatch(). But it basically works like this (using
your example url test.pl/notify/test):

1) look at the first part of the path, "notify", and run it through
translate_module_name() so that it becomes "Notify"
2) Add any defined prefix so that it becomes "Modules::Notify".
3) Modules::Notify is "require"d which means Perl will look in @INC for a file
named Modules/Notify.pm and that has a package name of Modules::Notify.

-- 
Michael Peters
Plus Three, LP



More information about the cgiapp mailing list