FJAX: Ajax with Flash

Ajax is a standart de-facto for Web 2.0 applications. Using Ajax technology it’s possible to create a flexible and handy web-based user interface. But the computer world is not at a standstill. The new web technology is coming: FJAX. FJAX is a Flash-based implementation of Ajax:

Fjax uses the Flash Player to load a 1 pixel by 1 pixel transparent SWF to simply get XML from the server. Once it has the XML, it parses it into HTML and then lets JavaScript know it’s ready. JavaScript then gets the HTML from Flash and DHTMLs it into the web page รขโ‚ฌโ€ it uses JavaScript to write (X)HTML/CSS onto the page.

I also started to use Ajax. I didn’t develop something serious yet. But that’s down the road ๐Ÿ™‚

[via O’Reilly Radar]

Lost in Translation

Today I tryed to implement a functionality to create some CRM entities from email. I needed to retrieve an email, first name and last name (if possible) and body of the email. The last name is a mandatory property. I made a mistake when I used perlish style to initialize the last name:
preg_match('/\"(.*)\"\s+\< (.*)\>/', $from, $from_arr);
$names = preg_split('/\s+/', $from_arr[1]);
$email = $from_arr[2];
$first_name = $names[0];
$email_user = preg_match('/^(.*?)@/', $email);
$last_name = $names[1] || $first_name || $email_user;

But PHP is not Perl. I got “1” as the last name. So, I changed it on PHP manner:
$last_name = $names[1];
if(!$last_name) $last_name = $name[0];
if(!$last_name) $last_name = $email_user[1];

IMHO Perl variant is much better ๐Ÿ™‚

Web Automation with HTTP::Recorder

Many of Perl programmers at least once used LWP or its extensions to create some web automation scripts. There are no any serious problems to do simple things. However, to simulate some complicated web procedure it may take a lot of programmer’s time. In this case, the module HTTP::Recorder can do all this work for you:

HTTP::Recorder is a browser-independent recorder that records interactions with web sites and produces scripts for automated playback. Recorder produces WWW::Mechanize scripts by default (see WWW::Mechanize by Andy Lester), but provides functionality to use your own custom logger.

The article “Web Testing with HTTP::Recorder”
from Perl.com detailed explains how to setup environment and use HTTP::Recorder. There is also a couple examples which can help you create your first web automation script.
I found this module quite interesting but I didn’t find time to test it. Hope I do it soon especially as I have a complicated web automation task.

Howto get the first day of the week by week number

Today I faced with small problem. I prepared a chart where data was split by weeks. It was very easy except one thing: display the first day of the week instead of week number. Here is a one way to do it (the main idea belongs to my co-worker Igor):
sub get_date_by_week {
my $week = shift || 1;
my $year = shift || (localtime)[5];
my $pattern = shift || '%d/%m';
my $t = timelocal(0, 0, 0, 1, 0, $year);
my $w = (localtime($t))[6];
$w = ($w - 2) % 7 + 1;
my $wc = $week * 7 - $w;
return strftime($pattern, localtime($t + $wc * 24 * 3600));
}

Don’t forget to add this in your script/module:
use Time::Local qw(timelocal);
use POSIX qw(strftime);

Note: this approach works correctly if the week starts from Monday!

Update: But today I found that PostgreSQL can trancate date by week and return the first day of the week:
SELECT date_trunc('week', TIMESTAMP '2006-06-01 10:38:40')::date
Result: 2006-05-29
So, now I can group by this date instead of week number!

use Gantry

Gantry is a new Perl Framework to develop web applications. It offers:

  • Plugin architecture
  • Support mod_perl 1.3 and 2.0
  • Support CGI and Fast-CGI
  • Support Template Toolkit and the ability to plugin other templating systems
  • Inherited framework code for a standard CRUD (Create, Read, Update, and Delete) application.
  • Calendaring support
  • Cookie management
  • Form generation and validation
  • User authentication: user, group and page-based
  • Class::DBI integration
  • Database and SQL utilities
  • AJAX Support
  • TinyMCE Support (a platform independent web based Javascript HTML WYSIWYG editor)
  • Code generation with BigTop – a Web Application Data Language processor

There are two existing, famous Perl MVC Frameworks: Maypole and Catalyst. Also the new one – Woodstock will be designed by Sebastian Riedel – the project founder of Catalyst and released soon. So, you have a choice when you decide to develop your web application on Perl.

Get backtrace in PHP

The inevitable has occurred! PHP developers added possibility to get backtrace! There are two functions: debug_backtrace and debug_print_backtrace. The first one returns an associative array each element of which contains following structure:
array(4) {
["file"] => string(10) "<file name>"
["line"] => int(10) "<line number>"
["function"] => string(6) "<function name>"
["args"]=>
array(1) {
<list of arguments passed to the function>
}

The second function prints the backtrace in Java manner. debug_backtrace is available since PHP 4.3.0 and debug_print_backtrace – PHP 5.
In my case those functions can help me to find which SugarCRM functions cause PearDatabse errors, for example. I use the similar approach in Perl a long time. Perl includes core function caller which returns a three elemets array contained full path name, file name and line number where specific subrouting was called from. To get a full backtrace I use Carp::confess which stop the programm and print the backtrace.

Update exiting RT2 Ticket

Today I spent some time trying to update a property of exiting Rt2 Ticket. I made a search in the class Ticket and didn’t find anything similar Update of Save. It turned out, that RT doesn’t use a specific method to store all modified object properties. To set a new value to objetc property it constructs the setter:
Set<propertyname>
Moreover, RT save the new value in the database immediately after calling the setter.
Here is a simple example how to set a new FinalPriority to the Ticket:
my $ticket = new RT::Ticket($curr_user);
$ticket->Load($ticket_id);
print "Priotiry befora update is " . $ticket->FinalPriority . "\n";
$ticket->SetFinalPriority(50);
print "Priotiry after update is " . $ticket->FinalPriority . "\n";

Perl is the best again

Perl core source code is tested using an open source software by Coverity, Inc:

Ben Chelf, Coverity’s CTO, commented that “of the LAMP stack, Perl had the best defect density, well past standard deviation and better than the average.”

Well, Perl demonstrated that it’s the best. At least in the LAMP (Linux, Apache, Mysql, Perl/PHP) area. But nobody knows about results of Java source code tests ๐Ÿ˜‰