简体   繁体   English

Perl Dancer如何管理表单动作

[英]Perl Dancer how to manage form actions

I'm learning perl Dancer and working on a to-do list depending on a form selection of two dates(today and tomorrow). 我正在学习perl Dancer,并根据两个日期(今天和明天)的形式选择工作清单。 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 : 我创建了一个名为舞蹈家应用: Organizador ,并已在我的下面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. 我已经创建了一个名为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>

How can I call create_to_do_list.pl as an action on template 'create_to_do_list'; 如何调用create_to_do_list.pl作为对template 'create_to_do_list'; after hitting the create button? 点击创建按钮后?

Thanks! 谢谢!

Firstly, before you go too far down this path, switch from Dancer to Dancer2. 首先,在您走得太远之前,请从Dancer切换到Dancer2。

From your comments, it seems that create_to_do_list.pl is a CGI program. 从您的评论看来, create_to_do_list.pl是一个CGI程序。 Is it running on the same web server? 它是否在同一台Web服务器上运行? You could probably call it remotely using something from LWP or HTTP::Tiny , but I don't think that's a very good idea - you'll get HTML back which you'll need to parse in some way to extract the useful information. 您可能可以使用LWPHTTP :: Tiny中的某些东西远程调用它,但我认为这不是一个好主意-您将获得HTML,您需要以某种方式进行解析以提取有用的信息。

It's a far better idea to move the code from create_to_do_list.pl into a module. 将代码从create_to_do_list.pl移到模块中是一个更好的主意。 If the CGI program needs to exist as well (for historical reasons, perhaps) then move the core code into a module which can be used from both the CGI program and the new Dancer app. 如果CGI程序也需要存在(也许是出于历史原因),则可以将核心代码移到一个模块中,该模块可以从CGI程序和新的Dancer应用程序中使用。 But if you won't need the CGI program once the Dancer app is ready, I'd just copy the code into the correct place in Organizador.pm. 但是,如果Dancer应用程序准备就绪后,您不需要CGI程序,则只需将代码复制到Organizador.pm的正确位置即可。

Instead of using DBI directly, you might find it easier to switch to 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 ). 与直接使用DBI相比,您可能会发现切换到Dancer :: Plugin :: Database (或与之等效Dancer2 )更容易,除了最简单的数据库程序以外,还可以使用其他任何东西,我建议使用DBIx :: Class (和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... 我想搬到Dancer,所以我认为有一种更快的调用脚本的方法,而不必复制它……我正在与成千上万的[CGI]待办事项清单...

Ideally, you should convert all of your CGI scripts to modules so that you can use them in non-CGI contexts (eg unit tests, web frameworks like Dancer and Mojolicious); 理想情况下,您应该将所有CGI脚本转换为模块,以便可以在非CGI上下文中使用它们(例如,单元测试,Dancer和Mojolicious之类的Web框架); however, if you really have thousands of them, that will take a long time. 但是,如果您确实有成千上万个,那将花费很长时间。

As a stop-gap measure while you work on the conversion, you can use CGI::Compile and CGI::Emulate::PSGI to create a PSGI wrapper around each of your unconverted CGI scripts. 作为进行转换时的权宜之计 ,您可以使用CGI :: CompileCGI :: Emulate :: PSGI围绕每个未转换的CGI脚本创建PSGI包装器。 You can easily integrate these with a Dancer2* app using Plack::Builder . 您可以使用Plack :: Builder轻松将它们与Dancer2 *应用程序集成。

For example, to integrate the following CGI script with a Dancer2 app: 例如,要将以下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;

Modify bin/app.psgi to look like this: 修改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);
};

Now, requests to / will call the / route in MyApp, while requests to /foo will call your CGI script. 现在,对/请求将调用MyApp中的/路由,而对/foo请求将调用您的CGI脚本。

In your form, change: 在您的表格中,更改:

<form action="create_to_do_list.pl">

to: 至:

<form action="/foo">

Make sure the names of your form fields all match what the CGI script is expecting, and voila! 确保表单字段的名称都与CGI脚本所期望的相匹配,瞧! You can keep using your CGI script without modification. 您可以继续使用CGI脚本而无需进行修改。

(Note that you could skip all the PSGI wrapper business and just continue serving your CGI scripts with Apache or whatever you were using before, but this approach allows you to centralize your routes and simplifies deployment.) (请注意,您可以跳过所有PSGI包装器业务,而仅继续使用Apache或以前使用的任何服务提供CGI脚本,但是这种方法可以使您集中路由并简化部署。)

Add a separate mount statement for each CGI script you want to integrate with your app. 为要与应用程序集成的每个CGI脚本添加单独的mount语句。 Note that this approach will probably have performance problems , so you should only use it as a temporary measure while you work on converting your CGI scripts to proper modules. 请注意,这种方法可能会出现性能问题 ,因此在将CGI脚本转换为适当的模块时,应仅将其用作临时措施。


* For new development, you should really be using Dancer2. *对于新开发,您确实应该使用Dancer2。 Dancer1 is in maintenance mode and although it's still officially supported, it won't be getting any new features. Dancer1处于维护模式,尽管仍得到官方支持,但不会获得任何新功能。 I know you've had trouble getting started with Dancer2, but you should resolve those issues instead of using an old version of the framework. 我知道您在使用Dancer2入门时遇到了麻烦 ,但是您应该解决这些问题,而不要使用旧版本的框架。 (And it's still unclear what exactly you were having trouble with; you should edit that question if you still need help.) (目前尚不清楚您到底在遇到什么问题;如果仍然需要帮助,则应编辑该问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM