Stories
Slash Boxes
Comments

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jozef (8299)

jozef
  (email not shown publicly)
http://search.cpan.org/~jkutej/
Jabber: jozef@kutej.net

Journal of jozef (8299)

Thursday July 17, 2008
06:15 AM

Scope::Guard for db rollback

For the first time I used Scope::Guard and I like the idea. I used it to always issue database rollback if something goes wrong, some exception, in DBIx::Class ResultSet function. Then when calling this function I can wrap it in eval {}; block and don't need to care about the begin/commit/rollback stuff.

sub do_some_database_stuff : ResultSet {
    my $rs   = shift;

    # begin transaction
    $rs->result_source->schema->txn_begin;

    # trigger rollback if we leave the function unexpecticly
    my $make_sure_we_rollback = Scope::Guard->new(sub{
        local $@;    # we don't want txn_rollback to mess up the $EVAL_ERROR
        $rs->result_source->schema->txn_rollback;
    });

    #
    # insert, deletes, updates ...
    #

    # commit transaction when all passed
    $rs->result_source->schema->txn_commit;
    $make_sure_we_rollback->dismiss;

    return $some_created_rows;
}

Monday June 16, 2008
02:27 PM

Text::ECSV

Roland Giersig had a presentation at YAPC 2007 called 'Techniques for Remote
System-Monitoring'. He was explaining his search after a good logging
format or how to store continuous flow of data in a most usable form.
XML? YAML? CSV? XML is nice but for a machines not for humans,
YAML is nice for both but it's hard to grep. CSV is readable and grep-able
but not too flexible. So what is the conclusion? ECSV is like a CSV but
in each comma separated field the name of the column is set. This gives a
flexibility to skip, reorder, add the fields. All the information is stored
per line so it's easy to grep. Also it's easy to compare two records by
md5-ing the lines or doing string eq.

For my next project I'm going to play with this idea so I've create new module
- http://search.cpan.org/perldoc?Text::ECSV. Basically all it does is
that in addition to the Text::CSV functionality it also gets the key/values
from the CSV fields and store them in a hash for easy accessing.

Friday June 06, 2008
11:42 AM

pod2docbook

As I was not able to reach author of Pod::DocBook v1.2 (UNAUTHORIZED RELEASE) Nandu Shah, I ressurected it from CPAN and created Pod::2::DocBook. This version futures many handy options that can be used to produce parts of docbook document, like sections, articles etc.

I use it my MakeFile this way:

pod/example.xml: $(PODROOT)/Path/To/Module.pm
        pod2docbook --title='example module POD' --no-header --doctype=section $ $@

and then I xinclude it to the docbook document.

Hopefully others will also take advantage of this module.

Tuesday February 26, 2008
04:24 PM

Catalyst book review

Accelerating Perl Web Application Development

A new book promising to teach:

  • Installing and setting up Catalyst
  • Using the Template toolkit to generate HTML output
  • Designing, accessing and working with databases
  • Using FormBuilder fto define auto-generating and self-validating forms
  • Using the session plug-in to add sessions to an application
  • Authentication and authorization, explored by implemented page-level and record-level control
  • Interfacing to your data
  • Adding REST API, AJAX interactivity, RSS feeds in your application
  • Automated testing and the "Test-Driven Development" methodology, discussed in depth
  • Writing tests for applications developed in this book with MEchanize and Selenium
  • Packaging you application

Quite a lot of topics for a 180 page work. But it's the first book for Perl about modern "hot" web stuff. The author of this book is Jonathan Rockway a member of the Catalyst Core Team.

The book it self is too short to go much deep into to the topics but shows a whole picture about Catalyst and the modules that are needed to make the web application work. The book contains a lot of information regarding the framework and I learned a few things that I couldn't found on CPAN. Here they are:

(p. 41) I found the usage of use base Catalyst::Controller::BindLex practical. Using it you don't have to manually put variables to the stash. Instead a simple my $var : Stashed = 'foo'; will automatically declare and propagate a variable to the stash.

(p. 45) There were more interesting things about DBIx::Class. For instance, the method ->find_or_new() which will either find the row in the database of create a new object. Another example is when properties are set/updated, calling ->update_or_insert() it, as it names implies, will insert or update the record into the database. This little neat tricks will reduce the amount of duplicated code.

(p. 66) I found out that ResultSet has a "build-in" pager: ->page(1), ->pager;

(p. 96) Jonathan was doing "magic" with resultsets, specially showing us how we can chain them and how to use ->search_related()

Reading this book made me realize that DBIx::Class deserves a book on it self. With a complete database design tutorial, examples, reasoning and best practices. With all this topics the book will easily make another 180 pages.

The examples in the book use FormBuilder to setup their forms. From what I have read, it looks like a powerful module. Specially that it's able to generate client side Java Script to validate forms. But this is not described too much in detail.

(p. 54) Regarding YAML and other types of configuration modules you can read only with all the other things. I found two things that can be done quite elegantly. The first is, if we want to pass certain configuration parameters to a model we can just create a configuration section that has the same name as the model to configure (ex. MyApp::DB) and all the variables in the configuration section will be accessible by $self->{key} in that particular model. Quite handy for passing database connection options for example.

(p. 162) The second thing, probably more interesting is that for a given main config file (ex. myapp.yml) we can create a second one with a the suffix '_local' (ex. myapp_local.yml). When this is done, the key-values from the '_local' are added/replaced to the main config file. Thus, we can have the defaults in the main and all the local "diverts" in the "local" without any fear of loosing configuration parameters when upgrading the application. In the past I created a module for this sole purpose.

(p. 57) A small but handy trick is __PACKAGE__->config( 'session' => { 'flash_to_stash' => 1 } ). If we turn on this feature all flash variables can be accessed like ordinary stash variables. Their special "flash" behaviour persists. Not writing ugly conditions, just like I did in the past :$.

And the last thing - is that REST and Jemplate used together will make AJAX. REST was quite interesting for me because it is using the HTTP protocol (GET, PUT, POST, DELETE) for remote communication. Nothing "new" but easy to debug and to use. The preferred content type can be set through HTTP headers (XML, JSON, YAML, ...). JSON's format can be easily used in AJAX for communication with a server. The folks that like using Template Toolkit, will like Jemplate as well. Jemplate uses TT format and prepares the templates for a Java Script on the client side. The TT files can be easily used for both static pages and AJAX part. Very clever and nice too!

I missed some intro to scaffolding which is a pride of Ruby on Rails. There are Catalyst::Helper classes that can do this but I had a bad luck when searching for help or tutorial about them (and this time I was too lazy to read the sources...).

I would appreciate a deeper Catalyst application design analysis. It's one thing to do short examples for a book and another one to start a big project with a good model.

To conclude. A good book, that's easy to read and that's worth to read when you think about WEB 2.0 and Perl. I hope there will be more books of the kind in the market!