新灵空间

I will go wherever god leads me to!

Dancer::Introduction Notes

1. A route pattern can contain one or more tokens, and tokens can be optional.

 

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

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

 

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

3. A route can be defined with a Perl regular expression. In order to tell Dancer to consider the route as a real regexp, the route must be defined explicitly with qr{}, like the following:

 

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

4. 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'
}

5. 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:

 

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};
};

6. Before hooks, after hooks, before_template_render hooks. 

7. You can use the load method to include additional routes into your application.

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

 

get '/appname' => sub {
    return "This is " . config->{appname};
};

9. In order to enable the logging system for your application, you first have to start the logger engine in your config.yml:

 

logger: 'file'

10. Dancer configures the Template::Toolkit engine to use <% %> brackets instead of its default [% %] brackets, although you can change this in your config file. Of course you have to import the "Template" module.  

11. A layout is a special view, located in the 'layouts' directory (inside the views directory) which must have a token named `content'. That token marks the place where to render the action view. This lets you define a global layout for your actions. Any tokens that you defined when you called the 'template' keyword are available in the layouts, as well as the standard session, request, and params tokens. This allows you to insert per-page content into the HTML boilerplate, such as page titles, current-page tags for navigation, etc. (refer to the standard document for more details).

12. Through  "set serializer => 'JSON';"  we can do data serializer;

Dancer::Introduction 学习记录

这不是自己第一次学习dancer这个Perl Web 框架了, 也不是第一次学 Dancer::Introduction 这个教程了。但是因为学习了太长时间, 忘得差不多了, 所以现在记录下, 以备查询。

1. 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.   

2. A route pattern can contain one or more tokens (a word prefixed with ':'). Tokens can be optional, for example:

 

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

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

 

get '/download/*.*' => sub {
	my ($file, $ext) = splat;
	return "file is ".$file." and ext is ".$ext;
};

4. In order to tell Dancer to consider the route as a real regexp, the route must be defined explicitly with qr{}, like the following:get qr{/regular/([\w]+)} => sub {

	my ($name) = splat;
	return "Hello $name";
};

5. 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'.

6. 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.  

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

8. You can use the load method to include additional routes into your application. load is just a wrapper for require, but you can also specify a list of routes files.

9. Dancer configures the Template::Toolkit engine to use <% %> brackets instead of its default [% %] brackets.

10. In order to render a view, just call the 'template' keyword at the end of the action by giving the view name and the HASHREF of tokens to interpolate in the view (note that the request, session and route params are automatically accessible in the view, named request, session and params):

 

use Dancer;
use Template;
 
get '/hello/:name' => sub {
    template 'hello' => { number => 42 };
};

11. A layout is a special view, located in the 'layouts' directory (inside the views directory) which must have a token named `content'. That token marks the place where to render the action view. This lets you define a global layout for your actions. Any tokens that you defined when you called the 'template' keyword are available in the layouts, as well as the standard session, request, and params tokens. This allows you to insert per-page content into the HTML boilerplate, such as page titles, current-page tags for navigation, etc. 

12. When setting up a serializer, a new behaviour is authorized for any route handler you define: any response that is a reference will be rendered as a serialized string, via the current serializer. 

关于这篇教程就写这么多了。