Steven Sun's Blog

A dream land of peace.

Catalyst 5.8 the Perl MVC Framework Notes

  1. catalyst.pl MyApp (this command will create the app)

  2. Inside the lib directory, there is a file named MyApp.pm, This file defines the namespace and inheritance that are necessary to make this a Catalyst application. It also contains the list of plugins to load application-­specific configurations. These configurations can also be defined in the myapp.conf file in the MyApp directory. However, if the same configuration is mentioned in both the files, then the configuration mentioned here takes precedence.

  3. The file lib/MyApp/Controller/Root.pm will handle all root level (/)URLs. This is where the code that generates the welcome page is located.
  4. The root directory will hold your templates and other non-­code support files. A subdirectory called /root/static is for static content such as images and stylesheets. Catalyst is set up to serve static files from this directory automatically (under the /static path), thanks toCatalyst::Plugin::Static::Simple.
  5. The script directory contains the scripts needed to run, test, and modify your application. To note here, the script myapp_create.pl, which is a version of catalyst.pl that’s customized for your application. It can create Models, Views, Controllers, tests, and many other things.
  6. The t directory is where your application’s automatic tests are stored. By default, you’ll have the following three tests:

    • 01app.t, which is a test that passes if your application compiles.
    • 02pod.t, which will pass if your Plain Old Documentation (POD)embedded API documentation, inside your application, is valid.
    • 03podcoverage.t, which tests that every public function in your application has some documentation.
  7. When a user makes a request using the browser, Catalyst will look for the appropriate method that can handle the request within packages called Controllers.

  8. In a controller, there are some automatically created stuff. “BEGIN {extends ‘Catalyst::Controller’; }” is to tell Moose/Perl that this module is a Catalyst Controller. “PACKAGE–>meta->make_immutable” ells Moose that this module will not change at runtime. This statement is necessary for performance gains and is good practice to mention in every Moose module. Modules must return true when they’re loaded,otherwise Perl will assume that the loading has failed and will die with an error message. 1 is always true, so it’s conventional to use it for this purpose.

  9. To create a view, we can issue the following command, ‘perl script/myapp_create.pl view TT TT’. The TT TT part of the previous command means to create a View called View/TT.pm (the first TT) based on the standard Catalyst::View::TT (the second TT). For our example, The next step is to create a template in the root/hello directory.We will name it index.tt. The root directory is the default place where Catalyst::View::TT will look for templates. This can be changed by configuration if you really need to.
  10. Variables that are passed between Controllers and Views are called stash.
  11. For the “sub index” part in a controller, if we change it to “sub index :Path :Args(1)”, it will accept the argument after index. The :Path attribute after index tells Catalyst that this method will handle URL requests that do not mention the method name such as /hello. The Args(1) attribute declares that this action expects one argument.
  12. If we specified :Local instead of :Path, then Catalyst would map the index action to handle a URL that looks like /hello/index. So, the URL looks like the following http://localhost/hello/index/Bonjour!, where Bonjour! is the argument that is being passed to the Controller. If we omitted the attributes (:Local, :Path, and so on), Catalyst would ignore the action entirely and it would be a normal Perl subroutine in the package. If we want to handle any URL with this method, then we can use the :Global attribute.
  13. We also changed the first line of the hello subroutine to receive the parameter within the method. Initially, it looked like
    my ( $self, $c ) = @;.
    This gets the first two arguments passed to the action by Catalyst, $self and $c. $self is a MyApp::Controller::Hello object and is not of much use right now. $c is the Catalyst context and contains all the information about our application and the current request (and therefore is very useful). Catalyst passes more than just $self and$c though, so we want to modify that line to read my ($self, $c, @args)=@
    ;. This will allow us to access the rest of the arguments via the @args array.

  14. The template that will be rendered is determined by the private name of your action.

  15. The stash is a data structure that exists throughout a single Catalyst request. Data you insert into the stash in an action will be available to the View (and other actions). Templates can only access variables that have been explicitly placed here, so it’s important to remember to put your useful data in the stash (otherwise it will be gone at the end of the subroutine in the Controller, instead of at the end of the request).

  16. You can automatically restart the server when necessary by running the server like
    perl script/myapp_server.pl -r -d.
    The -r will cause the server to restart when appropriate, and the -d will show debugging information, even if you’ve turned it off inside your application. This is especially useful as you will want to deactivate the hardcoded -Debug option in your MyApp.pm, once you are ready to deploy your application.

Notes on Perl

  1. 为了查看某一个模块的版本号, 在命令行下输入以下的内容:
    perl -MSome::Module -e ‘print Some::Module->VERSION’

New Year’s Resolution

好久没有对未来做些规划了。 总觉得日子就这样过吧。 也懒得去做些什么大的改变了。

总是在各种技术间跳来跳去, 没有熟练任何一种技术。 玩过Perl, Python, Ruby, Shell Script, PHP, Lisp。 但是真正在公司的服务器上部署过的代码只有用PHP还有Shell Script写的代码。

在我刚来公司的时候, 是想从事perl的代码的编写的工作。 但是自从进入公司后, 自己在Perl上花的时间实在是微乎其微的。

以至于自己都有点迷失方向了, 不知道该给自己立下什么目标。

在自己入职刚一年的时候, manager跟我说, 以后或许可以从事L2方面的工作, 即专职从事代码编写的工作, 当然也得写rule。 前提肯定是自己能证明有能力去做这件事情了。虽然现在自己也有大把的时候可以进行coding, 但是毕竟还是L1. 自己负责的那些PHP代码只有偶尔需要改动。

学了好几天的Catalyst了。 现在我要下定决心把Perl的两个框架给学好, 一个是Catalyst, 一个是Dancer。 这两个框架公司里都有用到的。但是感觉挺难的, 而我的那个L1的同事竟然说Dancer挺容易的。 我是有多蠢啊!!!!

2014年, 希望自己在工作上能够有如下的进步。 1. 在Perl上能稍微有点成绩。 毕竟从知道有perl这门语言到现在都已经有两年多了。 现在还是这么菜的水平, 完全说不过去的。 2. 彻底掌握Catalyst还有Dancer这两个框架。 3. 对于PHP, 因为基本上公司用的框架就在那里了, 希望在新的一年里能够在扎实下PHP代码编写的技巧。 4. 入职满两年的时候希望能够有底气跟manager说我想转到L2去。 5. 每天写技术博客, 或长或短, 记录下每天学到什么新的技术。 那样或许长进会更快点的。 6. 有不懂的刻苦钻研或者向同事请教。

好吧, 就写这么多了。 希望能好好审视下自己今天写下的这些东西。

When Can We Omit the Ampersand While Calling a Subroutine

If the compiler sees the subroutine definition before invocation, or if Perl can tell from the syntax that it’s a subroutine call, the subroutine can be called without an ampersand, just like a built-in function.

Some Useful Macos Commands

  1. 在iterm2里, 输入“open -a Mou”打开Mou编辑器, 输入open example.md or open -a Mou example.md来打开一个文件。

  2. 在iterm2里, 按住cmd+enter, 将会将iterm2最大化。

怎可妄想

一个人不成熟的标志, 或许就是, 有的东西明知不属于自己, 却还痴心妄想, 可笑, 可悲, 可叹.

奇技淫巧之vim匹配括号

今日在和同事交接代码的时候, 因为代码比较长, 所以出现了在看到一个括号的时候, 不知道与它匹配的括号在哪里。 当时使用的是vim, 后来换成了sublime text, 但是还是不知道怎么去找括号的另一半。现在我们就来看看, 怎么在vim中去匹配括号的另一半。

首先把光标定位在一个括号上,在normal模式中, 按shift+5, 亦即%来自动调转到另一个括号上。

Practical Vim –chap04 Visual Mode

  1. Vim’s Visual mode allows us to define a selection of text and then operate upon it.

  2. ways to enabling visual modes:

1
2
3
4
v Enable character-wise Visual mode
V Enable line-wise Visual mode
<C-v> Enable block-wise Visual mode
gv Reselect the last visual selection
  1. the ‘gv’ command isreselects the range of text that was last selected in Visual mode. No matter whether the previous selection was character-wise, line-wise, or block-wise, the gv command should do the right thing. The only case where it might get confused is if the last selection has since been deleted.

  2. if there is a html tag, like “three”, type vit in normal mode, will get you put your cursor into the ‘h’ character of the word “three”. vit means “visually select inside the tag”

Practical Vim –chap02 Normal Mode

  1. if there is a line and it is in normal mode, like “hello, everyone, this is steven sun”. now if you position your cursor on the “s” of “steven sun” and then type “c2w”, you will enter into the “insert mode” and will be prompted to enter some text.

  2. use dap to delete a paragraph, use daw to delete a word, use dal to delete a single character;

  3. we can type :h operator to find those information about operators in vim.

  4. an action is composed from an operator followed by a motion.

Network and Protocol Functions in PHP

  1. file_get_contents($url);

  2. urlencode() function.This function takes a string and converts it to the proper format for an URL

  3. parse_url($url)

  4. gethostbyname

  5. ftp_fget