Notes on ‘Practical Programming’
In Python, print will add a new line automatically.
Python loads modules only the first time they are imported. Internally, Python keeps track of the modules it has already seen; when it is asked to load one that’s already in that list, it just skips over it. This saves time and will be particularly important when you start writing modules that import other modules, which in turn import other modules—if Python didn’t keep track of what was already in memory, it could wind up loading commonly used modules like math dozens of times.
- When Python imports a module, it sets that module’s name variable to be the name of the module, rather than the special string “main”. This means that a module can tell whether it is the main program:
if __name__ == "__main__": print "I am the main program"else: print "Someone is importing me" - Lists are mutable;
- If we loop through a list, the variable is left holding its last value when the loop finishes.
- For Python’s built in function open, the result of open is not the contents of the file. Instead, open returns a file object whose methods allow the program to access the contents ofthe file.
- Read reads the whole file, readline reads the next line of text from the file.
- string.strip, which returns a copy of a string that has leading and trailing whitespace characters (spaces, tabs, and newlines) stripped away.
- deadlines will read the lines of the file and store them in a list.
Jquery Enlightenment Study Notes 1
- The central concept behind jQuery is, “find something, do something.” More specifically, select DOM element(s) from an HTML document and then do something with them using jQuery methods.
- Also we can extend the concept behind jQuery to include, first creating something new, selecting it, then doing something with it.
- Executing code when the DOM is ready, but before window.onload.
- Make sure all stylesheets are included before your jQuery code. Doing so will make sure all the elements in the DOM are correctly rendered before jQuery begins executing code.
- Typically we do not want to wait for the window.onload event. That is the point of using a custom event like ready() that will execute code before window loads, but after the DOM is ready to be traversed and manipulated.
- In fact, placing all JavaScript code at the bottom of a page is a proven performance strategy.
- When attaching events to DOM elements contained in a wrapper set, the keyword this can be used to refer to the current DOM element invoking the event.
- $(‘a’).get(0).title = ‘jQuery.com’; found become this: $(‘a’)[0].title = ‘jQuery.com’; Both allow access to the actual DOM element. For the square bracket notation, it i
- s faster because it uses native JavaScript to retrieve the element from an array, instead of passing it to a method.
Notes on Effective Perl Programming 2
- 循环while的条件语句是标量上下文环境, 而foreach的条件语句则是列表上下文环境。
- 当我们调用某个子程序或使用某个操作符时, 没有提供保存或使用其返回结果的目标时, 就是空上下文(void context)。
- 在子程序中, shift 操作会默认用@_作为参数。 在子程序之外, shift会把@ARGV作为默认参数。
- 在标量上下文中, grep返回符合条件的元素个数, 而不是元素列表。
Notes on Effective Perl Programming
- 如果担心原先的程序启用strict后运行不正常, 则可以在实际修改代码前, 先在命令行上试着启用strict看看:
1
| |
所谓列表, 就是一组有序的标量集合。 所谓数组, 就是一个存储着列表的变量. 对于数组, 我们可以将它放在标量上下文中计算, 但对于列表, 则没有相应的概念。而列表永远只是列表, 它没有标量解释。
逗号操作符在标量上下文中会返回最右边的元素。 所以 my $scalar = (‘aa’, ‘bb’, ‘cc’) 会将cc赋给$scalar.
在标量上下文环境中的列表赋值操作, 会返回负值操作符右边的元素个数。
my $elements = my @array = localtime; 使用这种方法可以计算下某个操作生成的列表中有几个元素。 由于这个规则是右结合的, 所以实际上中间被赋值的列表可以是空列表:
my $elements = () = local time; 如果要统计全局匹配的个数, 或者split函数返回的结果有多少个元素, 可以利用这个特性。
1 2 3 4 | |
Some Random Notes on the Dancer Framework
I write this post to keep notes of those little knowledge snippets when I am learning Dancer, the Perl web framework.
Templates all go into the views/ directory. Optionally, you can create a “layout” template which provides a consistent look and feel for all of your views.
If we have some code like “‘msg’ => get_flash()”, this means we have a msg field which displays a message to the user when an event happens like a new entry is posted, or the user logs in or out. It’s called a “flash” message because we only want to display it one time, not every time the / URL is rendered.
The uri_for directive tells Dancer to provide a URI for that specific route. You might ask why we don’t simply hardcode the /add URI in our application or templates. The best reason not to do that is because it removes a layer of flexibility on where to “mount” the web application. Although the application is coded to use the root URL / it might be better in the future to locate it under its own URL route – at that point we’d have to go through our application and the templates and update the URLs and hope we didn’t miss any of them. By using the uri_for Dancer method, we can easily load the application wherever we like and not have to modify the application at all.
We will use the format like “insert into entries (title, text) values (?, ?)” to be sure our application won’t be vulnerable to SQL injection.
If we have some like “any [‘get’, ‘post’] => ‘/login’ => sub {}”, here a GET for a human browsing to the URL and a POST for the browser to submit the user’s input to the web application.
session->destroy; is Dancer’s way to remove a stored session.
If we have some code like “set layout => ‘main’”, what this tells Dancer’s template engine is that it should look for a file called main.tt in myapp/views/layouts/ and insert the calls from the template directive into a template parameter called content.
The “before_template_render” provides a way to manipulate the template parameters before they’re passed to the engine for processing.
Oh My Love Lyrics
听到John Lennon的“Oh my love”, 歌词还有曲调给人一种非常空旷的感觉。现在就把歌词摘抄如下。
Oh my love for the first time in my life,
my eyes are wide open.
Oh my love for the first time in my life,
my eyes can see.
I see the wind, I see the trees, everything is clear in my heart.
I see the clouds, I see the sky, everything is clear in our world.
Oh my lover for the first time in my life, my mind is wide open.
Oh my lover for the first time in my life, my mind can feel.
I feel sorrow, I feel dreams, everything is clear in my heart.
I feel life, I feel love, everything is clear in our world.
Yaml Exception Reading Errors
When I use the “rake generate” command, sometimes I will get this error like, “YAML Exception reading”. We can solve this problem by following the method below.
1 2 | |
Catalyst Manual Tutorial Notes
Make sure when adding new plugins you also include them as a new dependency within the Makefile.PL file.
When specifying plugins, you can omit Catalyst::Plugin:: from the name.
You should refer to “Action-types” in Catalyst::Manual::Intro for additional information and for coverage of some lesser-used action types not discussed here (Regex and LocalRegex).
Although DBIx::Class has included support for a create=dynamic mode to automatically read the database structure every time the application starts, its use is no longer recommended.
lib/MyApp contains a Schema subdirectory, which then has a subdirectory called “Result”. This “Result” subdirectory then has files named according to each of the tables in our simple database (Author.pm, BookAuthor.pm, and Book.pm). These three files are called “Result Classes” (or “ResultSource Classes”) in DBIx::Class nomenclature. Although the Result Class files are named after tables in our database, the classes correspond to the row-level data that is returned by DBIC.
Also note the “flow” of the model information across the various files and directories. Catalyst will initially load the model from lib/MyApp/Model/DB.pm. This file contains a reference to lib/MyApp/Schema.pm, so that file is loaded next. Finally, the call to load_namespaces in Schema.pm will load each of the “Result Class” files from the lib/MyApp/Schema/Result subdirectory. The final outcome is that Catalyst will dynamically create three table-specific Catalyst models every time the application starts (you can see these three model files listed in the debug output generated when you launch the application).
The Catalyst stash only lasts for a single HTTP request. If you need to retain information across requests you can use Catalyst::Plugin::Session.
You cannot define a many_to_many relationship bridge without also having the has_many relationship in place.
At the end of a given user request, Catalyst will call the most specific end method that’s appropriate. For example, if the controller for a request has an end method defined, it will be called. However, if the controller does not define a controller-specific end method, the “global” end method in Root.pm will be called.
Catalyst takes “extra slash-separated information” from the URL and passes it as arguments in @_ (as long as the number of arguments is not “fixed” using an attribute like :Args(0)).
Chaining lets you have a single URL automatically dispatch to several controller methods, each of which can have precise control over the number of arguments that it will receive. A chain can essentially be thought of having three parts — a beginning, a middle, and an end.
In practice you should never use a GET request to delete a record — always use POST for actions that will modify data.
There are several ways to pass information across a redirect. One option is to use the flash technique.
It is generally recommended (partly for historical reasons, and partly for code clarity) only to use default in MyApp::Controller::Root, and then mainly to generate the 404 not found page for the application.
Flash allows you to set variables in a way that is very similar to stash, but it will remain set across multiple requests. Once the value is read, it is cleared (unless reset).
If you want to keep your existing method, you can create a new copy and comment out the original by making it look like a Pod comment. For example, put something like =begin before sub add : Local { and =end after the closing }.
For debugging techniques in Catalyst, please visit the page “https://metacpan.org/pod/Catalyst::Manual::Tutorial::07_Debugging”. We can also find information about how to “Check the version of an installed module” and how to “Check if a modules contains a given method”.
If you want to use MySQL or postgresql in catalyst, please refer to the following url, “https://metacpan.org/pod/Catalyst::Manual::Tutorial::10_Appendices”.
Catalyst 5.8 the Perl MVC Framework Notes–Creating a Database Model
- The command for creating a database model is as the following:
1
| |
- The first argument is the name of the Catalyst Model (TestDatabase). DBIC::Schema is what sort of model we’re creating. MyApp::Schema::TestDatabase is where the schema definition will be stored (we won’t use the Schema in this example, but real applications will). create=dynamic tells DBIC to read the database every time theapplication is started to determine the schema (layout of tables, foreign key relations, and so on.). The final argument is the DBI connect string for the database.