I'm learning perl Dancer and working on a to-do list depending on a form selection of two dates(today and tomorrow). If you select today a todo list for today will be generated, if you select tomorrow a different list will be created.
I've created a Dancer app called: Organizador
and have the following in my 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';
};
I've created a file called create_to_do_list.pl
which contains the script that I would like to execute when the form is created.
<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>
How can I call create_to_do_list.pl
as an action on template 'create_to_do_list';
after hitting the create button?
Thanks!
答案 0 :(得分:1)
首先,在你走太远之前,从Dancer切换到Dancer2。
根据您的评论,create_to_do_list.pl
似乎是一个CGI计划。它是否在同一个Web服务器上运行?您可以使用LWP或HTTP::Tiny中的某些内容远程调用它,但我认为这不是一个好主意 - 您将获得HTML,您需要以某种方式解析它提取有用的信息。
将代码从create_to_do_list.pl
移动到模块中是一个好主意。如果CGI程序也需要存在(也许是出于历史原因),那么将核心代码移动到可以从CGI程序和新的Dancer应用程序中使用的模块中。但是,如果您的Dancer应用程序准备就绪后您不需要CGI程序,我只需将代码复制到Organizador.pm中的正确位置。
除了直接使用DBI之外,您可能会发现更容易切换到Dancer::Plugin::Database(或其Dancer2 equivalent),除了最简单的数据库程序之外的任何其他内容,我建议{{3 (和DBIx::Class)。
答案 1 :(得分:1)
我想转移到Dancer所以我认为有一种更快的方式来调用我的脚本而不必复制它...我正在使用成千上万的[CGI]待办事项列表... < / p>
理想情况下,您应该将所有CGI脚本转换为模块,以便可以在非CGI上下文中使用它们(例如,单元测试,像Dancer和Mojolicious这样的Web框架);但是,如果你真的有成千上万,那将需要很长时间。
作为转换过程中的一个权宜之计,您可以使用CGI::Compile和CGI::Emulate::PSGI围绕每个未转换的CGI脚本创建一个PSGI包装器。您可以使用Plack::Builder轻松地将这些与Dancer2 *应用集成。
例如,要将以下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,但你应该解决这些问题,而不是使用旧版本的框架。 (目前还不清楚你究竟遇到了什么问题;如果你还需要帮助,你应该编辑这个问题。)