Perl and operator SWITCH

As you may know Perl doesn’t contain a SWITCH control operator. Instead, there are many ways to create SWITCH. This is a good and bad. Yesterday I fixed a bug in our Billing System related with custom implementation of SWITCH operator. Image we have a code like that:
my $var1 = '';
my $var2 = '';
SWITCH: {
# the first option
$var1 = 11;
$var2 = 12;
last SWITCH if $type == 1;
# the second option
$var1 = 21;
$var2 = 22;
last SWITCH if $type == 2;
}

This example works fine if $type is 1 or 2. We suppose to have variables $var1 and $var2 empty in case $type is 3, for example. But they will be 21 and 22 accordingly. To avoid this mistake we should add the last option at the end of our SWITCH block:
$var1='';
$var2='';

Personally, I don’t like this implementation of SWITCH block. I prefer more clear and obvious way, IMHO:
my $var1 = '';
my $var2 = '';
SWITCH: for($type) {
/^1$/ && do { $var1 = 11; $var2 = 12; last SWITCH; };
/^2$/ && do { $var1 = 21; $var2 = 22; last SWITCH; };
}

In this case assignment will be done correctly.
Perl includes the module Switch.pm since version 5.8.0. This module implements the operator SWITCH in the same manner as other programming languages:
use Switch;
switch ($type) {
case 1 { $var1 = 11; $var2 = 12; }
case 2 { $var1 = 21; $var2 = 22; }
else { print "Note: [$type] is not triggered!" }
}

Well, it’s your decision to use the standard SWITCH operator or create your own. But be carefully, it can produce strange bugs.

JavaScript, DHTML JavaScript, DHTML JavaScript DHTML Tooltips

Here is another useful JavaScript Cross Browser library – Tooltip. Using Tooltip you can add various tooltips on your web page even with URL or images (see image below). The library is full customizable and easy in use. Just download the library, place the file wz_tooltip.js somewhere in your web directory, load it:
<script language="JavaScript" type="text/javascript" src="wz_tooltip.js"></script>
and add desired tooltip in HTML tag:
<a href="index.htm" onmouseover="return escape('Some text')">Homepage</a>
For more information, please, read the library documentation.

Well, put togrther Prototype, Tooltips and customized function window.alert() and enjoy a rich and stylish user interface on your site.

Tooltips

Upload files to web with FileChucker

FileChucker is a Perl script supported Ajax which cab be used to upload and manage files on the web site. It includes a real-time progress bar, a built-in password protection, E-mail notification about new uploads, automatically replacement of any “unfriendly” characters, a possibility to work over SSL, an adjustable max upload size.

The FileChucker can be easyly integrated with existing web application even it’s based on PHP. You can test it online, read documentation or download it. Enjoy.

Customize JavaScript core functions

I started to change my mind. It seems that JavaScript is a cool and power. It allows to do many things. One of them is a possibility to iverride easyly any core function. The simple and clear example is here. You can use stylish and infomative alerts instead of default boring window.alert. The same manner you can customize any others functions to meet your requirements.

Web, Perl and Ajax

Prototype
I don’t like JavaScript. I prefer server-side technologies because they give a confidence in the correct work on any client PC. You cannot have the same confidence with JavaScript functionality. It has dependences on browser type, set browser options and many others conditions on the client-side. Hovewer, web 2.0 brought the modern realisation of client technology based on JavaScript – Ajax. Using Ajax it’s possible to develop an user-friendly web interface which is a standart de-facto now. I also started to study and test Ajax. There are many Ajax frameworks for the good-known programming languages, including Perl. The main goal of PerlAjax (CGI::Ajax) is to provide an easy way to create Perl Ajax applications without JavaScript coding.

The big disadvantage (IMHO) of CGI::Ajax is an orientation to the pure CGI. But it’s difficult or probably impossible to use this module together with Perl web site development and delivery engines such Embperl or Mason. Hovewer, there are a bunch of JavaScript Ajax frameworks which can be integrated with them. One of those ones is a Prototype. Its development is driven heavily by the Ruby on Rails framework, but it can be used in any environment. Here is a control for Mason which uses Prototype. In case of Embperl it’s needed some JavaSCript coding to bring Ajax power in the web application. But it’s not so big.

Mosaic images and video with Perl

Perl is cool and geek! You can do many thing using it and not just simple scripts. You have to just know how to do it. Tomorrow I found two articles on 128.ibm.com: “Create mosaic images with Perl and ImageMagick” and “Create mosaic movies with Perl, ImageMagick, and MPlayer“. Source code is available for both articles.

Perl with taste of Strawberry

Perl developers who work under Windows didn’t have possibility to install XS CPAN modules directly from CPAN. I used a command nmake from MS Visual Studio to do it. The project Strawberry Perl which is a part of Vanilla Perl Project changed this situation:

The purpose of the Strawberry Perl series is to provide a practical Win32 Perl environment for experienced Perl developers to experiment with and test the installation of various CPAN modules under Win32 conditions, and to provide a useful platform for doing real work.

In my mind Strawberry Perl appreciably lightens the life of Perl developers under Windows who don’t want to move to Linux.

Playing with Ruby

I need to add support a new interface panel to the my Plutohome installation. To do it I use Generic Serial Device (GSD) which works perfect. GSD has embedded Ruby interpretator to provide a tool for implementing desire device logic. In my case, I receive a hexdecimal binary string which contains a device ID, device port, command and data. All of them should be converted in integer exept data. The data should be converted in float. I found this solution how to convert hex string to float on Ruby forum:
irb(main):001:0> s='0x44361000'
=> "0x44361000"

irb(main):002:0> f=[s.to_i(16)].pack('L').unpack('F')[0]
=> 728.25

I like Ruby. It’s the similar to Perl but, of course, with some specificity.