Dancer::Introduction - A gentle introduction to Dancer

DESCRIPTION--->简介

Dancer is a free and open source micro web application framework written in Perl.

Dancer 是一个免费的开源的基于perl编程语言的微小web应用框架。

INSTALL --->安装

Installation of Dancer is simple:

使用cpanm来安装Dancer 模块

cpanm Dancer

安装非常的方便,并且cpanm的安装也非常的方便

借鉴 http://www.php-oa.com/2010/05/05/perl-cpanminus-cpan.html

SETUP--->设置

Create a web application using the dancer script:

使用dancer脚本创建一个web应用

dancer -a MyApp

执行完后会在本路径下生成一个目录MyApp

Run the web application:

运行创建的web应用

cd MyAppbin/bin

./app.pl

You can read the output of bin/app.pl --help to change any settings such as the port number.

如果想对app.pl有更多的了解,可以使用命令来查看帮助文档

./app.pl --help

这样你就可以了解到怎么来修改默认的端口,怎么转换到其他的web应用中去,并且也会懂得怎么样将dancer脚本与终端分离放置在background中运行。当然,输出的帮助信息不止这些。

最后,当dancer脚本成功运行后,会返回一个连接地址,然后你就可以通过这个连接地址去访问刚才搭建的web应用了。

View the web application at:

http://0.0.0.0:3000

USAGE--->使用方法

When Dancer is imported to a script, that script becomes a webapp, and at this point, all the script has to do is declare a list of routes. A route handler is composed by an HTTP method, a path pattern and a code block. strict and warnings pragmas are also imported with Dancer.

The code block given to the route handler has to return a string which will be used as the content to render to the client.

当dancer被引入到一个脚本后,这个脚本就变成了一个webapp(web应用),之后,脚本所要做的是声明一系列的ruotes(路由)。一个路由处理由三部分组成,分别是:

1)http 方法 (比如:通过http访问web时使用get还是post等方法)

2)路径样式(路径匹配)(比如:具体要访问web中的哪个目录下的哪个页面)

3)代码块 (比如:如果一切正常,那么web server将怎么处理这个web request,也就是web server需要对web request进行的操作,最后将什么内容附加返回给client端)。

当然,随着dancer被引入了脚本中,与之俱来的还有warnings 和 strict指令。

Routes are defined for a given HTTP method. For each method supported, a keyword is exported by the module.

对于每个给出的http方法,都有一条定义的路由,为了对每一个方法都支持,每个方法都对应一个关键字作为处理该请求的标示。

The following is an example of a route definition. The route is defined for the method 'get', so only GET requests will be honoured by that route:

下面是一个对get方法进行的route定义。

get '/' => sub {

# do something

return 'Hello world';

};

也只有get request才能通过该route handler来处理。

HTTP METHODS----->http 方法

Here are some of the standard HTTP methods which you can use to define your route handlers.

GET The GET method retrieves information (when defining a route handler for the GET method, Dancer automatically defines a route handler for the HEAD method, in order to honour HEAD requests for each of your GET route handlers). To define a GET action, use the get keyword.

POST The POST method is used to create a resource on the server. To define a POST action, use the post keyword.

PUT The PUT method is used to update an existing resource. To define a PUT action, use the put keyword.

DELETE The DELETE method requests that the origin server delete the resource identified by the Request-URI. To define a DELETE action, use the del keyword.

一些标准的可以在用来定定义route处理的http方法:

get(检索信息):当你为一个GET mothod定义了一个route处理的时候,dancer会自动的为HEAD method定义一个route handler,为什么?为了每个get route handler执行HEAD 请求。如果要定义一个Get 操作,需要使用get 关键字。

post(在server上创建一个资源)如果要定义一个POST操作,需要使用post关键字。

put (更新一个已经存在的资源),如果要定义一个put操作,需要使用put关键字。

delete (delete方法请求server删除uri请求中指定的资源),如果要定义一个delete操作,需要使用del关键字。

To define a route for multiple methods you can also use the special keyword any. This example illustrates how to define a route for both GET and POST methods:

如果想为多个方法定义一个路由,那么可以使用any关键字来组合,下面的例子说明了怎么为get和post方法来创建一个路由。

any ['get', 'post'] => '/myaction' => sub {

# code

};

Or even, a route handler that would match any HTTP methods:

更甚者,可以使用一个路由handler来处理任何的http方法

any '/myaction' => sub {

# code

};

ROUTE HANDLERS----->路由处理

The route action is the code reference declared. It can access parameters through the 'params' keyword, which returns a hashref. This hashref is a merge of the route pattern matches and the request params.

路由行为是声明过的代码引用(code reference),她可以通过'params'关键字访问参数,关键字'params'返回的是一个hashref(hash引用),该hashref融合了路由模式匹配(该路由模式匹配-->是不是就是上面说的路径匹配呢?和请求参数)。

You can have more details about how params are built and how to access them in the Dancer::Request documentation.

如果想了解有关params的更多知识请查看Dancer::Request documentation.

NAMED MATCHING----->命名匹配

A route pattern can contain one or more tokens (a word prefixed with ':'). Each token found in a route pattern is used as a named-pattern match. Any match will be set in the params hashref.

一个路由匹配可包含一个或者多个标志(单词前需要':'作前缀)。在一个路由匹配中的每个标志都被用作命令模式匹配。每个匹配会在params hashref中被设置。

get '/hello/:name' => sub {    'Hey '.param('name').', welcome here!';};

Tokens can be optional, for example:

标志也可以是选择性的(可选可不选=or):比如

get '/hello/:name?' => sub {    'Hello there ' . (param->{name} || "whoever you are!");};

WILDCARDS MATCHING----->通配符匹配

A route can contain a wildcard (represented by a '*'). Each wildcard match will be returned in an arrayref, accessible via the 'splat' keyword.

一个路由可以包含通配符(通过'*'来呈现)。每个通配符的匹配会保存到一个数组引用中,该数组引用可以通过关键字'splat'来访问。例如:

get '/download/*.*' => sub {    my ($file, $ext) = splat;    # do something with $file.$ext here};

REGULAR EXPRESSION MATCHING----->正则表达式匹配

A route can be defined with a Perl regular expression.

一个路由可以通过perl正则表达式来定义。

In order to tell Dancer to consider the route as a real regexp, the route must be defined explicitly with qr{}, like the following:

为了能通知并让dancer理解该路由是通过perl的正则表达式来定义的,该路由必须通过qr{}来准确的表达,否则不好使。例如:

get qr{/hello/([\w]+)} => sub {    my ($name) = splat;    return 'Hello $name';};

这样dancer在处理路由的时候,会将'([\w]+)'这部分通过perl的正则表达式来赋值,然后再继续处理。

CONDITIONAL MATCHING----->条件匹配

Routes may include some matching conditions (on the useragent and the hostname at the moment):

路由也可以包含一些匹配条件(用在用户代理和主机名方面)。

get '/foo', {agent => 'Songbird (\d\.\d)[\d\/]*?'} => sub {  'foo method for songbird'}

get '/foo' => sub {  'all browsers except songbird'}

PREFIX----->前缀

A prefix can be defined for each route handler, like this:

转载于:https://blog.51cto.com/perfect/931523

Dancer--introduction小议❶相关推荐

  1. dancer cookbook 小议3

    毁坏一个会话 When you're done with your session, you can destroy it: session->destroy Sessions and logg ...

  2. “HOW CAN WE KNOW THE DANCER FROM THE DANCE?”: COGNITIVE POETICS AND WILLIAM BUTLER【翻译】

    Pagel, Amber Noelle. "How Can We Know the Dancer from the Dance?": Cognitive Poetics and W ...

  3. Blender 3.0基础入门学习教程 Introduction to Blender 3.0

    成为Blender通才,通过这个基于项目的循序渐进课程学习所有主题的基础知识. 你会学到什么 教程获取:Blender 3.0基础入门学习教程 Introduction to Blender 3.0- ...

  4. 网络增强现实开发简介 Introduction to Web AR development

    搭配webXR.mindAR.three.js和tensorflow.js 你会学到: 获得构建不同类型的网络增强现实应用程序的实践经验,包括图像效果.人脸效果和世界效果 获得关于增强现实如何在网络浏 ...

  5. ZBrush全面入门学习教程 Schoolism – Introduction to ZBrush

    ZBrush全面入门学习教程 Schoolism – Introduction to ZBrush ZBrush全面入门学习教程 Schoolism – Introduction to ZBrush ...

  6. 视频色彩校正简介 Introduction to Video Color Correction

    视频色彩校正简介 Introduction to Video Color Correction 视频色彩校正简介 Introduction to Video Color Correction MP4 ...

  7. [转]Introduction of iSCSI Target in Windows Server 2012

    Introduction of iSCSI Target in Windows Server 2012 源地址:http://blogs.technet.com/b/filecab/archive/2 ...

  8. MS UI Automation Introduction

    MS UI Automation Introduction 2014-09-17 MS UI Automation是什么 UIA架构 UI自动化模型 UI自动化树概述 UI自动化控件模式概述 UI 自 ...

  9. 音频(3):iPod Library Access Programming Guide:Introduction

    Next Introduction 介绍 iPod库访问(iPod Library Access)让应用程序可以播放用户的歌曲.有声书.和播客.这个API设计使得基本播放变得非常简单,同时也支持高级的 ...

最新文章

  1. 面试官:小伙汁,你画的SpringMVC请求处理过程是从网上抄的吧?
  2. linux怎么删除端口转发,linux使用rinetd快速实现端口转发
  3. 如何遍历numpy数组?
  4. 28Python正则表达式、正则表达式对象、正则表达式修饰符、表达式模板、表达式实例、match函数、search方法、检索和替换、repl、compile、findall等
  5. python请输入_不断提示用户输入Python
  6. 2017微服务 mysql集群_成功升P7多亏掌握了这几点:高并发+Nginx+微服务+Redis+MySQL...
  7. 新闻发布项目——接口类(newsTbDao)
  8. 前端学习(1687):前端系列javascript基础面试前言
  9. Android 使用ViewPager实现导航页面
  10. Windows下安装python2和python3双版本
  11. Struts如何获取客户端ip地址
  12. iperf命令linux,Linux iperf 用法介绍
  13. vb mysql 实例_vb数据库编程实例-求VB连接数据库实例我想做一个VB连接数据库的简单实例,可以实现 爱问知识人...
  14. 《区块链技术与应用》公开课系列笔记——目录导航页
  15. python报告水印怎么弄_python 图片添加水印 pdf 添加水印
  16. 微信聊天记录怎么备份?
  17. 制作一个四轮四驱底盘【内附资料下载链接】
  18. pta Mysql题目集 (81-100)
  19. java smtp.126.com_Java Mail---SMTP、POP3协议-DOS下手动收发邮件演示过程
  20. 企业级数据管理——DAMA数据管理

热门文章

  1. UnboundLocalError: local variable ‘list‘ referenced before assignment
  2. 使用高斯混合模型(GMM)近似未知分布:EM算法的应用
  3. 设置git命令使用的简写、缩写
  4. 【Vim】学习笔记day02 Vim的三级窗体切换
  5. MogaFX—老挝央行禁止货币兑换单位出售外币
  6. Python脚本解析BitTorrent种子文件内容
  7. Macbook pro终端中文乱码
  8. 连续相乘计算机公式,Excel中如何批量算乘法?一个公式即可搞定所有乘法!
  9. vlookup如何将符合条件的多个数据全部查找出来
  10. Marathon主要功能介绍