Category Archives: Web development

All web based development (Server side programming, client side tool/languages, frameworks etc)

ORM for existing projects using Phrasebook

Just few questions came in mind so I thought to get some expert advice. Well, I am not an DB expert but comfortable with SQL/Perl DBI and can write queries using Phrasebook. I mostly use PostgresSQL DB. As there is a lot of discussion of ORM and many new implementations are coming using ORM techniques (e.g.; DBIx::Class etc) for DB interaction so just wondering how can I use ORM in my existing projects as I am still using DBI + Phrasebook and what benefit shall I get if I switch to ORM?

Thanks for reading!

Fork me on GitHub

MojoMojo and Selenium test

As suggested by Mateu I was further looking into MojoMojo Selenium tests on CPAN. Actually all selenium.t tests require a test configuration file mojomojo.yml file resides in t/app/mojomojo.yml this file is used to add the authentication configurations. Also resides in the same directory the sqlite mojomojo database: mojomojo.db.

The complete test is as follows:

#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;

# This test requires that a Selenium server be running.  A Selenium server
# is included in Alien::SeleniumRC which is a dependency of
# Test::WWW::Selenium::Catalyst so having it installed should be
# sufficient to run this test.
#
# The selenium server is a java application that can also be started like:
#     java -jar selenium-server.jar
# See http://seleniumhq.org/ for the download of Selenium Remote Control (RC)
# which includes a selenium server.

BEGIN {
    eval {require Test::WWW::Selenium::Catalyst};
    plan skip_all => 'Selenium tests need Test::WWW::Selenium::Catalyst'
        if $@;
    plan skip_all => 'Set SELENIUM_TESTS ENV variable  to enable Selenium tests'
        unless $ENV{SELENIUM_TESTS};
    plan tests => 22;
    $ENV{MOJOMOJO_CONFIG} = 't/app/mojomojo.yml';
}

Test::WWW::Selenium::Catalyst->import('MojoMojo');
my $sel = Test::WWW::Selenium::Catalyst->start;

$sel->open_ok("/");
$sel->is_text_present_ok("Log in");
$sel->open_ok("admin.profile");
$sel->is_text_present_ok("Log in");
$sel->open_ok(".recent");
$sel->is_text_present_ok("Log in");
$sel->open_ok(".list");
$sel->is_text_present_ok("Log in");
$sel->click_ok("link=Log in");
$sel->wait_for_page_to_load_ok( "15000");
$sel->type_ok( "loginField", "admin" );
$sel->type_ok( "pass",       "admin" );
$sel->click_ok("//input[@value='Login']");
$sel->wait_for_page_to_load_ok("15000");
$sel->is_text_present_ok("admin");

# Check that .recent was not cached.
$sel->open_ok(".recent");
$sel->is_text_present_ok("Log out");

# Check that profile was no cached.
$sel->open_ok("admin.profile");
$sel->is_text_present_ok("Log out");
$sel->open_ok(".list");
$sel->is_text_present_ok("Log out");
$sel->click_ok("link=Log out");

Reference: MojoMojo on CPAN

Testing Javascript in Perl applications

There is a tool called Selenium which is used to test the Javascript component in Perl based applications. This is used to run tests directly in the browser. Actually this tool sits between Perl application, test script and browser. The test script sends the Selenium server commands to run inside the web browser. The web browser then executes those commands, and returns the results to Selenium server and then to test script. It requires JRE and a working web server to work with.

Jonathan Rockway in his book has explained and used this in Catalyst applications.

Data Serialization in Perl examples

One can store data and objects into a file using Serialization. The bytes that is generated can then be re-used to generate an object that will be identical (a clone) to the stored object. We can achieve this by using Storable module from CPAN. The example below will show a way to store and retrieve such objects.

#!/usr/bin/perl

use 5.006;
use strict;
use warnings;
use Data::Dumper;
use Storable;

my %hash1 = ('Test1' => '10',
	     'Test2' => '20');

store %hash1, "serialized_file";
print "##### HASH1 #####n";
print Dumper(%hash1);

my %hash2 = %{retrieve "serialized_file"};
print "n##### HASH2 #####n";
print Dumper(%hash2);

exit 0;

Output:

##### HASH1 #####
$VAR1 = {
          'Test2' => '20',
          'Test1' => '10'
        };

##### HASH2 #####
$VAR1 = {
          'Test2' => '20',
          'Test1' => '10'
        };


For more info see perldoc Storable
Serialization is used to store data and objects into a file for instance. The serie of bytes that is generated can then be re-used to generate an object that will be identical (a clone) to the stored object.

PDL A way to deal with larger arrays

If you are working in scientific domain or using bioperl where you require to deal with bulk data processing then you should seriously consider learning PDL (“Perl Data Language”). PDL is actually a way to deal with larger arrays in Perl.  It allows large N-dimensional data sets such as large images, spectrogram, etc to be stored efficiently and manipulated quickly.

To say it with the words of Karl Glazebrook, initiator of the PDL project:

    “The PDL concept is to give standard perl5 the ability
    to COMPACTLY store and SPEEDILY manipulate the large
    N-dimensional data sets which are the bread and butter
    of scientific computing. e.g. $a=$b+$c can add two
    2048x2048 images in only a fraction of a second.”

 

PDL is well suited for matrix computations, general handling of multidimensional data, image processing, general scientific computation, and numerical applications. It supports I/O for many popular image and data formats including 1D (line plots), 2D (images) and 3D (volume visualization, surface plots via OpenGL) etc.

Latest stable release of PDL is PDL-2.4.7 and can be found at CPAN or Sourceforge.

Happy learning.

Perl 6 documentation and screencast

Perl 6 is new language from Perl community. As always good documentation plays a key role in understanding new language at faster pace. Perl 6 has plenty of resources on official documentation page available on Perl foundation.

I also found some interesting screen casts by Gabor Szabo which were indeed helpful to understand Perl 6 data structures.

Happy learning!

More backtracking examples

Backtracking might be costly so one should try to avoid useless backtracking. Perl regx have  special form of parentheses: (?>…). These are called Perl’s “don’t-ever-backtrack-into-me” markers. They will tell the regex engine that the enclosed sub-pattern can safely be skipped over during backtracking. As we know that the re-matching the contents either won’t succeed or, if it does succeed, won’t help the overall match. So these markers helps to avoid useless backtracking and saves a lot of time.

Some more useful links and examples can be found at:

http://codenode.com/2010/02/28/debugging-perl-regex-backtracking/

Perl Regx documentation page Perlre on Perldoc

Perl Best Practices by Damian Conway