免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5011 | 回复: 10
打印 上一主题 下一主题

Catalyst中的这个冒号是什么意思 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-24 09:44 |只看该作者 |倒序浏览
sub do_post : Local {
    my ($self, $c) = @_;
    $c->form(
      validate => [['EQUAL_TO',$c->captcha_string]]
    )
  }

论坛徽章:
0
2 [报告]
发表于 2008-05-24 10:25 |只看该作者
同问

论坛徽章:
0
3 [报告]
发表于 2008-05-24 11:55 |只看该作者
method的attribute
以前见过lock和lvalue
声明为lvalue的则sub的结果可以做为一个作值返回,直接的后果就是你可以给这个sub赋值
Local不知 ,难道返回的结果是一个Local类型的值?

论坛徽章:
0
4 [报告]
发表于 2008-05-24 12:03 |只看该作者

回复 #3 churchmice 的帖子

这是在http://www.perlchina.org/上面看到的

关于:的语法解释,可以在具体点不,还是第一次见到

论坛徽章:
0
5 [报告]
发表于 2008-05-24 12:25 |只看该作者

回复 #4 sotol 的帖子

我错了
仔细看了一下
那个是catalyst里面特有的
共有Global Local Private三种属性
我也不懂

论坛徽章:
0
6 [报告]
发表于 2008-05-24 12:32 |只看该作者
看这个
http://dev.catalyst.perl.org/docs/Catalyst/Manual/Intro.html

  1. Action types

  2. Catalyst supports several types of actions:
  3. Literal (Path actions)
  4.     package MyApp::Controller::My::Controller;
  5.     sub bar : Path('foo/bar') { }

  6. Literal Path actions will act relative to their current namespace. The above example matches only http://localhost:3000/my/controller/foo/bar. If you start your path with a forward slash, it will match from the root. Example:
  7.     package MyApp::Controller::My::Controller;
  8.     sub bar : Path('/foo/bar') { }

  9. Matches only http://localhost:3000/foo/bar.
  10.     package MyApp::Controller::My::Controller;
  11.     sub bar : Path { }

  12. By leaving the Path definition empty, it will match on the namespace root. The above code matches http://localhost:3000/my/controller.
  13. Regex
  14.     sub bar : Regex('^item(\d+)/order(\d+)$') { }

  15. Matches any URL that matches the pattern in the action key, e.g. http://localhost:3000/item23/order42. The '' around the regexp is optional, but perltidy likes it. :)

  16. Regex matches act globally, i.e. without reference to the namespace from which it is called, so that a bar method in the MyApp::Controller::Catalog::Order::Process namespace won't match any form of bar, Catalog, Order, or Process unless you explicitly put this in the regex. To achieve the above, you should consider using a LocalRegex action.
  17. LocalRegex
  18.     sub bar : LocalRegex('^widget(\d+)$') { }

  19. LocalRegex actions act locally. If you were to use bar in MyApp::Controller::Catalog, the above example would match urls like http://localhost:3000/catalog/widget23.

  20. If you omit the ``^'' from your regex, then it will match any depth from the controller and not immediately off of the controller name. The following example differs from the above code in that it will match http://localhost:3000/catalog/foo/widget23 as well.
  21.     package MyApp::Controller::Catalog;
  22.     sub bar : LocalRegex('widget(\d+)$') { }

  23. For both LocalRegex and Regex actions, if you use capturing parentheses to extract values within the matching URL, those values are available in the $c->req->captures array. In the above example, ``widget23'' would capture ``23'' in the above example, and $c->req->captures->[0] would be ``23''. If you want to pass arguments at the end of your URL, you must use regex action keys. See URL Path Handling below.
  24. Top-level (Global)
  25.     package MyApp::Controller::Foo;
  26.     sub foo : Global { }

  27. Matches http://localhost:3000/foo. The function name is mapped directly to the application base. You can provide an equivalent function in this case by doing the following:
  28.     package MyApp::Controller::Root
  29.     sub foo : Local { }
  30. Namespace-Prefixed (Local)
  31.     package MyApp::Controller::My::Controller;
  32.     sub foo : Local { }

  33. Matches http://localhost:3000/my/controller/foo.

  34. This action type indicates that the matching URL must be prefixed with a modified form of the component's class (package) name. This modified class name excludes the parts that have a pre-defined meaning in Catalyst (``MyApp::Controller'' in the above example), replaces ``::'' with ``/'', and converts the name to lower case. See Components for a full explanation of the pre-defined meaning of Catalyst component class names.
  35. Chained
  36. Catalyst also provides a method to build and dispatch chains of actions, like
  37.     sub catalog : Chained : CaptureArgs(1) {
  38.         my ( $self, $c, $arg ) = @_;
  39.         ...
  40.     }
  41.     sub item : Chained('catalog') : Args(1) {
  42.         my ( $self, $c, $arg ) = @_;
  43.         ...
  44.     }

  45. to handle a /catalog/*/item/* path. For further information about this dispatch type, please see the Catalyst::DispatchType::Chained manpage.

  46. Private
  47.     sub foo : Private { }

  48. Matches no URL, and cannot be executed by requesting a URL that corresponds to the action key. Private actions can be executed only inside a Catalyst application, by calling the forward method:
  49.     $c->forward('foo');

  50. See Flow Control for a full explanation of forward. Note that, as discussed there, when forwarding from another component, you must use the absolute path to the method, so that a private bar method in your MyApp::Controller::Catalog::Order::Process controller must, if called from elsewhere, be reached with $c->forward('/catalog/order/process/bar').
  51. Args
  52. Args is not an action type per se, but an action modifier - it adds a match restriction to any action it's provided to, requiring only as many path parts as are specified for the action to be valid - for example in MyApp::Controller::Foo,
  53.   sub bar :Local

  54. would match any URL starting /foo/bar/. To restrict this you can do
  55.   sub bar :Local :Args(1)

  56. to only match /foo/bar/*/


  57. Note: After seeing these examples, you probably wonder what the point is of defining names for regex and path actions. Every public action is also a private one, so you have one unified way of addressing components in your forwards.

  58. Built-in Private Actions

  59. In response to specific application states, Catalyst will automatically call these built-in private actions in your application class:
  60. default : Private
  61. Called when no other action matches. Could be used, for example, for displaying a generic frontpage for the main app, or an error page for individual controllers.

  62. If default isn't acting how you would expect, look at using a Literal Path action (with an empty path string). The difference is that Path takes arguments relative from the namespace and default always takes arguments relative from the root, regardless of what controller it's in. Indeed, this is now the recommended way of handling default situations; the default private controller should be considered deprecated.

  63. index : Private
  64. index is much like default except that it takes no arguments and it is weighted slightly higher in the matching process. It is useful as a static entry point to a controller, e.g. to have a static welcome page. Note that it's also weighted higher than Path.

  65. begin : Private
  66. Called at the beginning of a request, before any matching actions are called.

  67. end : Private
  68. Called at the end of a request, after all matching actions are called.


  69. Built-in actions in controllers/autochaining
  70.     Package MyApp::Controller::Foo;
  71.     sub begin : Private { }
  72.     sub default : Private { }
  73.     sub auto : Private { }

  74. You can define built-in private actions within your controllers as well. The actions will override the ones in less-specific controllers, or your application class. In other words, for each of the three built-in private actions, only one will be run in any request cycle. Thus, if MyApp::Controller::Catalog::begin exists, it will be run in place of MyApp::begin if you're in the catalog namespace, and MyApp::Controller::Catalog::Order::begin would override this in turn.
  75. auto : Private
  76. In addition to the normal built-in actions, you have a special action for making chains, auto. Such auto actions will be run after any begin, but before your action is processed. Unlike the other built-ins, auto actions do not override each other; they will be called in turn, starting with the application class and going through to the most specific class. This is the reverse of the order in which the normal built-ins override each other.


  77. Here are some examples of the order in which the various built-ins would be called:
  78. for a request for /foo/foo
  79.   MyApp::begin
  80.   MyApp::auto
  81.   MyApp::Controller::Foo::default # in the absence of MyApp::Controller::Foo::Foo
  82.   MyApp::end
  83. for a request for /foo/bar/foo
  84.   MyApp::Controller::Foo::Bar::begin
  85.   MyApp::auto
  86.   MyApp::Controller::Foo::auto
  87.   MyApp::Controller::Foo::Bar::auto
  88.   MyApp::Controller::Foo::Bar::default # for MyApp::Controller::Foo::Bar::foo
  89.   MyApp::Controller::Foo::Bar::end

  90. The auto action is also distinguished by the fact that you can break out of the processing chain by returning 0. If an auto action returns 0, any remaining actions will be skipped, except for end. So, for the request above, if the first auto returns false, the chain would look like this:
  91. for a request for /foo/bar/foo where first auto returns false
  92.   MyApp::Controller::Foo::Bar::begin
  93.   MyApp::auto
  94.   MyApp::Controller::Foo::Bar::end

  95. An example of why one might use this is an authentication action: you could set up a auto action to handle authentication in your application class (which will always be called first), and if authentication fails, returning 0 would skip any remaining methods for that URL.

  96. Note: Looking at it another way, auto actions have to return a true value to continue processing! You can also die in the auto action; in that case, the request will go straight to the finalize stage, without processing further actions.
复制代码

论坛徽章:
0
7 [报告]
发表于 2008-05-24 14:04 |只看该作者
还是没搞懂,sub bar :Local,我是想更多了解一点这样的函数声明是个什么语法,比如一般看到的函数声明是sub bar {}是这样的,也就是它们之间的区别吧

论坛徽章:
0
8 [报告]
发表于 2008-05-24 18:52 |只看该作者
就是来声明一个属性。属性可以自定义,你要编程指定这个属性的含义。

论坛徽章:
0
9 [报告]
发表于 2008-05-24 18:56 |只看该作者
原帖由 福瑞哈哥 于 2008-5-24 18:52 发表
就是来声明一个属性。属性可以自定义,你要编程指定这个属性的含义。

能给举个例子 详细讲讲吗

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
10 [报告]
发表于 2008-05-24 18:58 |只看该作者
原帖由 hitsubunnu 于 2008-5-24 18:56 发表

能给举个例子 详细讲讲吗

举个例子:比如一楼那个。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP