[SP-pm] CGI::Application::Dispatch

Paulo paulo at odos.com.br
Wed May 20 06:50:05 PDT 2009


Prezados da lista, estou estudando um despachador de requisições chamado
CGI::Application::Dispatch

http://search.cpan.org/~markstos/CGI-Application-Dispatch-2.16/lib/CGI/Application/Dispatch.pm

Pela documentação eu deveria receber as variáveis constantes nas regras 
de despacho através do mecanismo $self->param() no meu módulo 
CGI::Application. Isto não está acontecendo! Mais abaixo, onde repasso a 
saída do console, podemos observar que a url bate com a regra mas as 
variáveis não são repassadas quando o módulo é chamado.

Já vasculhei o google, as listas pertinentes, a lista de bugs e nada. 
Alguém tem alguma dica? Estou fazendo alguma bobagem óbvia?


Segue trecho da documentação:
--------------------------------------------------------
variable
Any token which begins with a colon (:) is a variable token. These are 
simply wild-card place holders in the rule that will match anything in 
the URL that isn't a slash. These variables can later be referred to by 
using the $self->param mechanism. In the rule

    'posts/:category'

:category is a variable token. If the URL matched this rule, then you 
could retrieve the value of that token from whithin your application 
like so:

    my $category = $self->param('category');
--------------------------------------------------------


Segue exemplo de teste colocado no /cgi-bin/teste.pl:
--------------------------------------------------------
#!/usr/bin/perl -w

use strict;
use warnings;
use diagnostics;

use lib '..';

# para simular a rota quando pela linha de comando
$ENV{'PATH_INFO'}='/cgi_teste/show';

use CGI::Application::Dispatch;

CGI::Application::Dispatch->dispatch(
    debug=>1,
    prefix      => 'My',
    table => [
        ':app/:rm'  => {},
    ],
);
--------------------------------------------------------


Segue exemplo do módulo:
--------------------------------------------------------
package My::Cgi::Teste;

use strict;
use base qw(CGI::Application);

sub setup {
    my $c = shift;
    $c->start_mode('show');
    $c->mode_param('rm');
    $c->run_modes([qw(show)]);
};


sub show {
    my $c = shift;
    my @p = $c->param();
    unshift @p, '>>>';      # para mostrar que algo foi impresso
    my $s .= join ' ', @p;  # monta string
    
    # retorna html simples
    return "<html><body>".$s."</body></html>";
}

1;
--------------------------------------------------------




Segue a saída do console
--------------------------------------------------------
[Dispatch] Trying to match '/cgi_teste/show/' against rule ':app/:rm' 
using regex '/([^/]*)/([^/]*)/'
[Dispatch] Matched!
[Dispatch] Named args from match: $VAR1 = {
          'rm' => 'show',
          'app' => 'cgi_teste'
        };

[Dispatch] loading module My::Cgi::Teste
[Dispatch] Final args to pass to new(): $VAR1 = {
          'PARAMS' => {}
        };

[Dispatch] creating instance of My::Cgi::Teste
Content-Type: text/html; charset=ISO-8859-1

<html><body>>>></body></html>
--------------------------------------------------------







More information about the SaoPaulo-pm mailing list