Category Archives: perl

Error With Error Module

Providing support for a web application to run on any web server on any hosting provider is a challenging task. No two hosing solutions probably have the same configurations. Even within the same hosting provider, one server might have configuration different from another.

To triage some of the issues related to the configuration, I have a script. The script is supposed to find test if a required perl module for the application is present or not on the website’s server. The script uses the Error.pm perl module to try and catch the exception and based on that reports if the module is present or not. Today, this script itself failed for someone. It turned out, who would have thought, that the Error.pm module itself was missing for this user. Given Error.pm provides some very basic try/catch syntax that’s widely used, this should have been a mandatory perl module. As a side note, the other day when I was migration some of my stuff from an old Linux box to the iMac, I found out that the Error.pm module is absent and when I installed it, it pulled in several dependencies. May be because of these dependencies, some web hosting providers don’t have it installed by default.

Advertisements

Leave a comment

Filed under perl

Porting Perl CGI Script From Linux Apache To Windows IIS

I am a linux guy. I mean, my laptop is windows based and all, but all the development is done on Linux. I prefer Java and Swing for desktop applications and Perl or Java for Web applications. I have a perl cgi script that I had to port it to IIS because a potential customer wanted it on Windows 2003 Server with IIS.

I have a laptop with Windows Vista Home Premium and so I wanted to try and figure out how to make the cgi script to work on IIS. After a lot of searching and researching, I figured out enabling IIS, installing ActivePerl, configuring to run the perl scripts and all. This article is not about how that can be done, but about what issue I ran into when porting the script.

First I thought I had to change the “#!/usr/bin/perl” to the appropriate perl interpreter path on Windows, but the way it works in IIS is that a specific url pattern can be mapped to a handler and program of that handler is configured. So, “*.pl” can be mapped to a specific program and windows will execute that program passing the script as input. This makes life easier, no need to maintain two separate versions of the program one for each platform.

However, the key issue I ran into is the error in loading the shipped modules. In Apache, a perl script is run with the current working directory set to the directory where the script is located. So, all the other modules of the program are resolved with respect to the current working directory and as a result there is no need for any special include path setting. But in IIS, this isn’t the case. The script is executed with the current working directory (BTW, pwd in linux and chdir in Windows) set to the virtual directory of the script. So, if the script is within a folder within the virtual directory, all the modules packaged with the script won’t be found.

So, I ended up doing the following to make it work with both Apache and IIS.

At the very beginning of the script, even before the modules are loaded, I have the following code.

BEGIN {
  $SCRIPT_PATH = $ENV{PATH_TRANSLATED}; # this env variable is available in IIS and not Apache
  if($SCRIPT_PATH) {
     $SCRIPT_PATH =~ s/[^\\\/]+$//;
     push @INC,$SCRIPT_PATH;
  }
  else {
     $SCRIPT_PATH = ".";
  } 
}

That’s it. This code will include the directory of the script into the include path so that the subsequent “use” and “require” statements are resolved appropriately.

One last note. If you have file opening logic in the script using a relative path, that should also be changed to resolve it w.r.t the $SCRIPT_PATH.

2 Comments

Filed under Apache, IIS, perl

perl: String hashCode

I had a need to create unique numbers out of strings. So, I explored the option of using hashcodes (which won’t be 100% unique especially if they need to be represented in limited number of bits). I couldn’t find any function suitable within perlfunc. After doing a bit of research on the web and not wanting to spend too much time understanding the theory, I just looked at java.lang.String source code and the hashcode function was like

hash = 0;
for(character in string) {
hash = 31*hash+character;
}

So far so good. When I tried this with perl, I got completely different answer. So, it turns out this is because, by default perl doesn’t use integer arithmetic in calculations. But you can force it to do so with “use integer;” and most importantly, this behavior can be controlled by block. So, here is my final perl String hashCode function that is same as Java’s implementation of a String hashCode.

sub hashCode {
my $hash = 0;
use integer;
foreach(split //,shift) {
$hash = 31*$hash+ord($_);
}
return $hash;
}

That’s it.

3 Comments

Filed under perl

User Input Sanitizing Before Using In Regular Expression

Say you want to take the input from a user and filter any set of text (titles/descriptions) and show the reduced list to the user. Assuming the users are not sophisticated enough to write regular expressions, the requirement is simply to ensure that whatever the user typed in is available some where within the text.

A simple way to do this is just look for the exact substring and see if the index is greater than or equal to 0 (something like str.indexOf(input) >= 0).

Say, you want to return a match that is case-insensitive. Then the substring approach will not work. So, you need to jump to using regular expressions. In JavaScript, this would become,

var re = new RegExp(input,"i"); // the flag i indicates case insensitive
if(re.test(str)) { /* do-some-thing-here */ }

So far, so good. Now, what happens if the user types in some special characters that are typically used in regular expressions as some special control characters? For example,

‘(‘ and ‘)’ are used for grouping and variable capturing
‘[‘ and ‘]’ are used for character set
‘{‘ and ‘}’ are used to indicate the cardinality
‘\’ is escape character
‘*’ is used to indicate 0 or more
‘+’ is used to indicate 1 or more
‘?’ is used to indicate 0 or 1

In this scenario, if someone types in a string with any of the above characters, then the above javascript will fail. So, in order to fix this, the user input string should be first fixed to make it a valid regular expression. This can be done using

var rere = new RegExp("[({[^$*+?\\\]})]","g"); /* you need 2 '\' s to mean 1 '\' and another '\' to treat ']' as special character instead of the characters ending bracket */
var reinput = input.replace(rere,"\\$1"); /* replace the special characters with a \ before them */
var re = new RegExp(reinput,"i");
if(re.test(str)) { ... }

Now if you are generating the above JavaScript code in perl, it gets a bit more complicated. Why? Because, ‘\’s are themselves have escape semantics. Also, $ symbol has special meaning in perl. So, for each ‘\’ above, it would be doubled and also, $ would be escaped as well.

Yes, it gets confusing, but it’s doable. You can see this in action at Flat Panel Plasma/LCD HDTVs. It contains a search box on the top of the image cloud and lets the user to input a string such as Panasonic, Samsung, Sony etc or even product numbers with special characters like TH-42PZ700U to find the corresponding product on the image.

Leave a comment

Filed under javascript, perl, regular expressions, Tech - Tips