加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Dancer--introduction小议?

发布时间:2020-12-16 00:21:09 所属栏目:大数据 来源:网络整理
导读:每个路由处理可以有一个定义前缀,如: prefix?'/home';? From?here,?any?route?handler?is?defined?to?/home/* 从这里开始,每个路由处理被定义到处理 /home/ 下的内容。 get?'/page1'?=?sub?{};?#?will?match?'/home/page1'? 当定义了上面的前缀后,上述 g

每个路由处理可以有一个定义前缀,如:

prefix?'/home';?

From?here,?any?route?handler?is?defined?to?/home/*

从这里开始,每个路由处理被定义到处理/home/下的内容。

get?'/page1'?=>?sub?{};?#?will?match?'/home/page1'?

当定义了上面的前缀后,上述get的处理就是/home/page1的内容

You?can?unset?the?prefix?value

你也可以取消一个前缀。

prefix?'/';?#?or:?prefix?undef;

通过?prefix?'/'或者perfix?undef的方法来取消定义的前缀,

get?'/page1'?=>?sub?{};?will?match?/page1?

当取消定义的前缀后,该get就会去处理/page1的内容。

Alternatively,?to?prevent?you?from?ever?forgetting?to?undef?the?prefix,?you?can?use?lexical?prefix?like?this:

或者,由于某些原因你可能会忘记了取消上面的前缀,然后继续编写你的代码,最后发现,想要的结果始终没有得到,这时你该思考是不是dancer的处理路径不对?why?is?it?so

what's?wrong?等等的N多的whywhat出现在了你的脑海,这时也许你会去花费很大一部分时间才能排除错误。但是永远要记住,每个成熟的语言在上市的时候,肯定也会将尽可能出现错误的地方明确的告诉了学习者,避免程序员在编写代码的时候由于临时错误或者临时思考不足而犯下严重的错误,dancer同样如此,为了避免上述情况的发生,dancer提供了另一种方法,让你不用担心在编写代码的时候由于忘记undef前缀而犯下严重错误,你可以将前缀放置在一个范围内来放置后续的代码都会使用这个前缀。这样就不会由于忘记undef前缀而犯错。

prefix?'/home'?=>?sub?{

??get?'/page1'?=>?sub?{

??};?

??#?will?match?'/home/page1'

};?##?prefix?reset?to?previous?value?on?exit

在前缀的后面紧跟着一个代码段定义,然后在代码段中使用了一个route?handler这样就可以将prefix局限在该代码段中,而不是整个代码中。

由于下面的route?handler不在上述的prefix声明内,所以此时的/page1就是/page1,而不是上面的/home/page1了。

?get?'/page1'?=>?sub?{};?will?match?/page1?

ACTION?SKIPPING------>动作跳过(也就是说,对应该请求的动作可执行,可不执行。)

An?action?can?choose?not?to?serve?the?current?request?and?ask?Dancer?to?process?the?request?with?the?next?matching?route.

This?is?done?with?the?pass?keyword,?like?in?the?following?example

一个动作(行为)可以选择不服务当前的请求而去请求dancer用下一个匹配路由来处理当前的请求,如果要实现这个要求,可以通过pass关键来来操作。如下:

get?'/say/:word'?=>?sub?{

???return?pass?if?(params->{word}?=~?/^d+$/);????'I?say?a?word:?'.params->{word};

};?

get?'/say/:number'?=>?sub?{????'I?say?a?number:?'.params->{number};};?

DEFAULT?ERROR?PAGES----->默认错误页面

When?an?error?is?rendered?(the?action?responded?with?a?status?code?different?than?200),?Dancer?first?looks?in?the?public?directory?for?an?HTML?file?matching?the?error?code?(eg:?500.html?or?404.html).

当出现一个错误(动作就会回复一个非200的状态码),dancer首先会去public目录下寻找匹配该错误状态码的HTML文件(如500.html?or?404.html)

If?such?a?file?exists,?it's?used?to?render?the?error,?otherwise,?a?default?error?page?will?be?rendered?on?the?fly.

如果有这样的文件存在,那么就用该文件的内容来回复用户,否则,会用一个默认的错误页回复(自动生成的?)

EXECUTION?ERRORS------>执行错误

When?an?error?occurs?during?the?route?execution,?Dancer?will?render?an?error?page?with?the?HTTP?status?code?500.

当在route执行过程中出现一个错误时,dancer会回复一个http?状态码为500的错误页面。

It's?possible?either?to?display?the?content?of?the?error?message?or?to?hide?it?with?a?generic?error?page.

该页面也许会显示route执行错误得到的错误信息也许会通过一个普遍的错误页面来隐藏route执行的错误信息。

This?is?a?choice?left?to?the?end-user?and?can?be?set?with?the?show_errors?setting.

该不该显示是由你来决定,你可以通过show_errors选项来设置。

Note?that?you?can?also?choose?to?consider?all?warnings?in?your?route?handlers?as?errors?when?the?setting?warnings?is?set?to?1.

请记住,你可以选择通过设置warnings=1来讲路由处理中出现的所有的警告作为errors来看待。

HOOKS------>钩?what?不懂

Before?hooks------>前钩子

Before?hooks?are?evaluated?before?each?request?within?the?context?of?the?request?and?can?modify?the?request?and?response.?It's?possible?to?define?variables?which?will?be?accessible?in?the?action?blocks?with?the?keyword?'var'.

前钩子能够在评估每个请求的内容之前修改请求和响应。可以通过var关键字定义变量,然后这些变量可以在代码块中被使用。

hook?'before'?=>?sub?{

????var?note?=>?'Hi?there';????

request->path_info('/foo/oversee')

};?

get?'/foo/*'?=>?sub?{

???my?($match)?=?splat;?#?'oversee';???

???vars->{note};?#?'Hi?there'

};?

For?another?example,?this?can?be?used?along?with?session?support?to?easily?give?non-logged-in?users?a?login?page:

另一个例子,应用到会话支持中来很容易的做到检测用户的登陆。

hook?'before'?=>?sub?{

????if?(!session('user')?&&?request->path_info?!~?m{^/login})

{????????

#?Pass?the?original?path?requested?along?to?the?handler:?

var?requested_path?=>?request->path_info;????????

request->path_info('/login');????

}

};?

The?request?keyword?returns?the?current?Dancer::Request?object?representing?the?incoming?request.?See?the?documentation?of?the?Dancer::Request?module?for?details.

request关键字会将进入的请求以Dancer::Request的对象来返回,欲了解更多请观看Dancer::Requestperldoc

After?hooks----->后钩子

after?hooks?are?evaluated?after?the?response?has?been?built?by?a?route?handler,?and?can?alter?the?response?itself,?just?before?it's?sent?to?the?client.

显然,后钩子与前钩子是相反的,后钩子会在路由处理产生的响应后进行评估的,并且能够响应发送给客户端之前改变响应。

The?hook?is?given?the?response?object?as?its?first?argument:

此时,后钩子将作为响应对象的第一个参数。

hook?'after'?=>?sub?{????

my?$response?=?shift;????

$response->{content}?=?'after?hook?got?here!';

};?

Before?template?hook------>前模板钩子

before_template_render?hooks?are?called?whenever?a?template?is?going?to?be?processed,?they?are?passed?the?tokens?hash?which?they?can?alter.

当一个模板将要被处理的时候会调用前模板转换钩子,他们被传递到标志hash中来修改。

hook?'before_template_render'?=>?sub?{????

my?$tokens?=?shift;????$tokens->{foo}?=?'bar';

};?

The?tokens?hash?will?then?be?passed?to?the?template?with?all?the?modifications?performed?by?the?hook.?This?is?a?good?way?to?setup?some?global?vars?you?like?to?have?in?all?your?templates,?like?the?name?of?the?user?logged?in?or?a?section?name.

然后,标志hash附加钩子所做的所有修改传递给模板,这是一个在所有模板中设置全局变量的好方法,比如:用户登录名或者区域名字。

CONFIGURATION?AND?ENVIRONMENTS------>配置和环境

Configuring?a?Dancer?application?can?be?done?in?many?ways.?The?easiest?one?(and?maybe?the?dirtiest)?is?to?put?all?your?settings?statements?at?the?top?of?your?script,?before?calling?the?dance()?method.

可以通过多种方法来配置一个dancer应用,最简单的一种(也许是最没有技术含量的一种)是将所有的设置指令放置在脚本的最上边,在调用dance方法之前。

Other?ways?are?possible,?you?can?write?all?your?setting?calls?in?the?file?'appdir/config.yml'.?For?this,?you?must?have?installed?the?YAML?module,?and?of?course,?write?the?conffile?in?YAML.

其他的方法也是可以的,你可以将所有的设置调用保存在'appdir/config.yml'文件中,这样的话,你必须安装YAML模块,当然要该文件也要以YAML的格式来编辑。

That's?better?than?the?first?option,?but?it's?still?not?perfect?as?you?can't?switch?easily?from?an?environment?to?another?without?rewriting?the?config.yml?file.

这种方法比第一种要好一点,但是还是不尽完美,因为在不重写config.yml文件的情况下你无法很容易的从一个环境转换到另一个环境中去。

The?better?way?is?to?have?one?config.yml?file?with?default?global?settings,?like?the?following:

比较好的一种方法是:设置一个默认的全局的config.yml?文件。如下:

#?appdir/config.yml

logger:?'file'

layout:?'main'

And?then?write?as?many?environment?files?as?you?like?in?appdir/environments.?That?way,?the?appropriate?environment?config?file?will?be?loaded?according?to?the?running?environment?(if?none?is?specified,?it?will?be?'development').

然后在appdir/environments目下编辑环境文件,这样,根据运行的环境相应的环境配置文件会被自动的加载。如果没有指定环境,那么默认是development环境。

Note?that?you?can?change?the?running?environment?using?the?--environment?commandline?switch.

请记住:你可以在命令行中通过--environment命令行开关来改变运行的环境。

Typically,?you'll?want?to?set?the?following?values?in?a?development?config?file:

通常,你会在development配置文件中设置如下变量:

#?appdir/environments/development.yml

log:?'debug'

startup_info:?1

show_errors:??1?

在生成环境中设置如下:

And?in?a?production?one:

#?appdir/environments/production.yml

log:?'warning'

startup_info:?0

show_errors:??0?

load----->加载方法

You?can?use?the?load?method?to?include?additional?routes?into?your?application:

你可以使用load方法将其他的路由加载到你的应用中。

get?'/go/:value',?sub?{????

#?foo

};?

load?'more_routes.pl';?

#?then,?in?the?file?more_routes.pl:

get?'/yes',?sub?{????

'orly?';

};?

load?is?just?a?wrapper?for?require,?but?you?can?also?specify?a?list?of?routes?files:

对于这种要求load方法只是一个封装,但是你可以在这个封装中指定一列路由文件。

load?'login_routes.pl',?'session_routes.pl',?'misc_routes.pl';?

Accessing?configuration?data------>访问配置文件数据

A?Dancer?application?can?access?the?information?from?its?config?file?easily?with?the?config?keyword:

对于一个dancer的应用可以简单的通过config关键字来访问config文件中的信息。

get?'/appname'?=>?sub?{????

return?'This?is?'?.?config->{appname};

};?

上述代码能够列出appname这个应用的配置文件的信息。

Importing?just?the?syntax------->导入?仅仅导入的是语法

If?you?want?to?use?more?complex?file?hierarchies,?you?can?import?just?the?syntax?of?Dancer.

如果你想使用更复杂的文件组织模式,你可以使用dancer语法

package?App;?

use?Dancer;???????????? ???#?App?may?contain?generic?routes

use?App::User::Routes;? ???#?user-related?routes?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读