Perl Dancer 如何管理表单动作
Perl Dancer how to manage form actions
我正在学习 perl Dancer 并根据两个日期(今天和明天)的形式 select 处理待办事项列表。如果你今天 select 将生成今天的待办事项列表,如果你明天 select 将创建一个不同的列表。
我创建了一个名为:Organizador
的 Dancer 应用程序,并在我的 Organizador.pm
中包含以下内容:
package Organizador;
use Dancer ':syntax';
use DBI;
our $VERSION = '0.1';
set session => "Simple";
get '/' => sub{
template 'index';
};
get '/create_to_do_list'=>sub{
template 'create_to_do_list';
};
我创建了一个名为 create_to_do_list.pl
的文件,其中包含我希望在创建表单时执行的脚本。
<form action="create_to_do_list.pl">
<legend>Create todo list</legend>
<label for="todoList">Create a todo list</label>
<select name='todoList' id='todoList'>
<option value="today">today</option>
<option value="tomorrow">tomorrow</option>
</select>
<button>Cancel</button>
<button>Create</button>
</form>
如何在点击创建按钮后调用 create_to_do_list.pl
作为对 template 'create_to_do_list';
的操作?
谢谢!
首先,在你走得太远之前,从 Dancer 切换到 Dancer2。
根据您的评论,create_to_do_list.pl
似乎是一个 CGI 程序。它 运行 在同一个网络服务器上吗?您可能可以使用 LWP or HTTP::Tiny 中的内容远程调用它,但我认为这不是一个好主意 - 您将得到 HTML 返回,您需要以某种方式解析以提取有用的信息。
将代码从 create_to_do_list.pl
移动到模块中是一个更好的主意。如果 CGI 程序也需要存在(可能出于历史原因),则将核心代码移动到一个模块中,该模块可以从 CGI 程序和新的 Dancer 应用程序中使用。但是,如果 Dancer 应用准备就绪后您不需要 CGI 程序,我只需将代码复制到 Organizador.pm.
中的正确位置
与其直接使用 DBI,您可能会发现切换到 Dancer::Plugin::Database (or its Dancer2 equivalent), bit for anything other than the simplest of database programs, I'd recommend DBIx::Class (and Dancer2::Plugin::DBIC) 更容易。
I wanted to move to Dancer so I thought there was a faster way of calling my script instead of having to copy it...I'm working with thousands of thousand of [CGI] to-do lists...
理想情况下,您应该将所有 CGI 脚本转换为模块,以便您可以在非 CGI 上下文中使用它们(例如单元测试、Web 框架,如 Dancer 和 Mojolicious);但是,如果您真的有数千个,那将需要很长时间。
作为转换过程中的权宜之计,您可以使用 CGI::Compile and CGI::Emulate::PSGI to create a PSGI wrapper around each of your unconverted CGI scripts. You can easily integrate these with a Dancer2* app using Plack::Builder。
例如,将以下 CGI 脚本与 Dancer2 应用程序集成:
use strict;
use warnings 'all';
use CGI;
my $q = CGI->new;
print $q->header,
$q->start_html,
$q->h1('Hello, CGI!'),
$q->end_html;
将 bin/app.psgi
修改为如下所示:
use strict;
use warnings 'all';
use FindBin;
use lib "$FindBin::Bin/../lib";
use CGI::Compile;
use CGI::Emulate::PSGI;
use Plack::Builder;
use MyApp;
my $foo_cgi = CGI::Compile->compile('/path/to/foo.cgi');
builder {
mount '/' => MyApp->to_app;
mount '/foo' => CGI::Emulate::PSGI->handler($foo_cgi);
};
现在,对 /
的请求将调用 MyApp 中的 /
路由,而对 /foo
的请求将调用您的 CGI 脚本。
在您的表单中,更改:
<form action="create_to_do_list.pl">
至:
<form action="/foo">
确保您的表单字段的名称都与 CGI 脚本所期望的相匹配,瞧!您可以继续使用您的 CGI 脚本而无需修改。
(请注意,您可以跳过所有 PSGI 包装器业务,只继续使用 Apache 或您之前使用的任何东西来提供 CGI 脚本,但这种方法允许您集中路由并简化部署。)
为每个要与您的应用程序集成的 CGI 脚本添加单独的 mount
语句。请注意,此方法可能会有 performance problems,因此在将 CGI 脚本转换为适当的模块时,您应该仅将其用作临时措施。
* 对于新的开发,你真的应该使用 Dancer2。 Dancer1 处于维护模式,虽然它仍然得到官方支持,但不会获得任何新功能。我知道您已经 had trouble 开始使用 Dancer2,但您应该解决这些问题而不是使用旧版本的框架。 (目前还不清楚你到底遇到了什么问题;如果你仍然需要帮助,你应该编辑那个问题。)
我正在学习 perl Dancer 并根据两个日期(今天和明天)的形式 select 处理待办事项列表。如果你今天 select 将生成今天的待办事项列表,如果你明天 select 将创建一个不同的列表。
我创建了一个名为:Organizador
的 Dancer 应用程序,并在我的 Organizador.pm
中包含以下内容:
package Organizador;
use Dancer ':syntax';
use DBI;
our $VERSION = '0.1';
set session => "Simple";
get '/' => sub{
template 'index';
};
get '/create_to_do_list'=>sub{
template 'create_to_do_list';
};
我创建了一个名为 create_to_do_list.pl
的文件,其中包含我希望在创建表单时执行的脚本。
<form action="create_to_do_list.pl">
<legend>Create todo list</legend>
<label for="todoList">Create a todo list</label>
<select name='todoList' id='todoList'>
<option value="today">today</option>
<option value="tomorrow">tomorrow</option>
</select>
<button>Cancel</button>
<button>Create</button>
</form>
如何在点击创建按钮后调用 create_to_do_list.pl
作为对 template 'create_to_do_list';
的操作?
谢谢!
首先,在你走得太远之前,从 Dancer 切换到 Dancer2。
根据您的评论,create_to_do_list.pl
似乎是一个 CGI 程序。它 运行 在同一个网络服务器上吗?您可能可以使用 LWP or HTTP::Tiny 中的内容远程调用它,但我认为这不是一个好主意 - 您将得到 HTML 返回,您需要以某种方式解析以提取有用的信息。
将代码从 create_to_do_list.pl
移动到模块中是一个更好的主意。如果 CGI 程序也需要存在(可能出于历史原因),则将核心代码移动到一个模块中,该模块可以从 CGI 程序和新的 Dancer 应用程序中使用。但是,如果 Dancer 应用准备就绪后您不需要 CGI 程序,我只需将代码复制到 Organizador.pm.
与其直接使用 DBI,您可能会发现切换到 Dancer::Plugin::Database (or its Dancer2 equivalent), bit for anything other than the simplest of database programs, I'd recommend DBIx::Class (and Dancer2::Plugin::DBIC) 更容易。
I wanted to move to Dancer so I thought there was a faster way of calling my script instead of having to copy it...I'm working with thousands of thousand of [CGI] to-do lists...
理想情况下,您应该将所有 CGI 脚本转换为模块,以便您可以在非 CGI 上下文中使用它们(例如单元测试、Web 框架,如 Dancer 和 Mojolicious);但是,如果您真的有数千个,那将需要很长时间。
作为转换过程中的权宜之计,您可以使用 CGI::Compile and CGI::Emulate::PSGI to create a PSGI wrapper around each of your unconverted CGI scripts. You can easily integrate these with a Dancer2* app using Plack::Builder。
例如,将以下 CGI 脚本与 Dancer2 应用程序集成:
use strict;
use warnings 'all';
use CGI;
my $q = CGI->new;
print $q->header,
$q->start_html,
$q->h1('Hello, CGI!'),
$q->end_html;
将 bin/app.psgi
修改为如下所示:
use strict;
use warnings 'all';
use FindBin;
use lib "$FindBin::Bin/../lib";
use CGI::Compile;
use CGI::Emulate::PSGI;
use Plack::Builder;
use MyApp;
my $foo_cgi = CGI::Compile->compile('/path/to/foo.cgi');
builder {
mount '/' => MyApp->to_app;
mount '/foo' => CGI::Emulate::PSGI->handler($foo_cgi);
};
现在,对 /
的请求将调用 MyApp 中的 /
路由,而对 /foo
的请求将调用您的 CGI 脚本。
在您的表单中,更改:
<form action="create_to_do_list.pl">
至:
<form action="/foo">
确保您的表单字段的名称都与 CGI 脚本所期望的相匹配,瞧!您可以继续使用您的 CGI 脚本而无需修改。
(请注意,您可以跳过所有 PSGI 包装器业务,只继续使用 Apache 或您之前使用的任何东西来提供 CGI 脚本,但这种方法允许您集中路由并简化部署。)
为每个要与您的应用程序集成的 CGI 脚本添加单独的 mount
语句。请注意,此方法可能会有 performance problems,因此在将 CGI 脚本转换为适当的模块时,您应该仅将其用作临时措施。
* 对于新的开发,你真的应该使用 Dancer2。 Dancer1 处于维护模式,虽然它仍然得到官方支持,但不会获得任何新功能。我知道您已经 had trouble 开始使用 Dancer2,但您应该解决这些问题而不是使用旧版本的框架。 (目前还不清楚你到底遇到了什么问题;如果你仍然需要帮助,你应该编辑那个问题。)