Check password quality with Perl

When you manage some serious system where users can change their passwords you’ll face with problem of quality of those passwords. There is a Perl module – Data::Password, which may do all work to check user passwords: length or the number of character groups. Also, it’s possible to make more complicated checking: the password does not contain the same chars repeatedly or ascending or descending characters, or charcters close to each other in the keyboard. You can use system dictionary files (/usr/share/dict/words for Fedora Core 5, for example) or create you own. The module has only two methods and six variable which can be imported into your script/module to customize checking. Here is a small example of using Data::Password:
use Data::Password qw(:all);
$GROUPS = 0;
# Will print "Bad password - contains the word 'clear', only lowercase"
print IsBadPassword("clearant");

Perl and BIG projects

I came across this post on Perlmonks. One monk asked other monks about examples of BIG project based on Perl. I don’t want to digg thesis “BIG project” because, in my mind, a measurement of the project quantity in the number of lines is not so accurate. The main problem when we’re talking about Perl and Enterprise applications is in lack of knowledge of appropriate tools.

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 = '';
# 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:

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 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.

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.

Web, Perl and Ajax

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 “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.

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 🙂