≡ Menu

Perl

Howto write a better and efficient Perl program

This is an excellent list of top five things that every Perl programmer should be aware of. A great article with good tips.

From the article:

Inside every tangle of obfuscated Perl code is a clean, well-architected gem struggling to emerge from its cocoon. brian d foy has spent a lot of time thinking about this for his new book, Mastering Perl, and has come up with a Top Five list of things that every Perl programmer should be thinking about when writing code.

Five Ways to Improve Your Perl Programming [onlamp.com]

You may have noticed that most shell and perl script starts with the following line:
#!/bin/bash
OR
#!/usr/bin/perl

It is called a shebang. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash. All scripts under UNIX and Linux execute using the interpreter specified on a first line.

However there is a small problem. BASH or Perl is not always in the same location (read as PATH) such as /bin/bash or /usr/bin/perl. If you want to make sure that script is portable across different UNIX like operating system you need to use /usr/bin/env command.

env command allows to run a program in a modified environment.

Find line
#!/bin/bash

Replace with
#!/usr/bin/env bash

For example here is a small script:

#!/usr/bin/env bash
x=5
y=10
echo "$x and $y"

OR

#!/usr/bin/env perl
use warnings;
print "Hello " x 5;
print "\\n";

Now you don’t have to search for a program via the PATH environment variable. This makes the script more portable. Also note that it is not foolproof method. Always make sure you have /usr/bin/env exists or use a softlink/symbolic link to point it to correct path. And yes your work (script) looks more professional with this hack :)

Perl script to monitor disk space and send an email

Here is a quick question by one of our regular reader :

How to write a perl script that can monitor my disk space under UNIX or Linux and send me an email alert?

There is a nice perl system routine called Perl df or Filesys::DiskSpace. This routine displays information on a file system such as its type, the amount of disk space occupied, the total disk space and the number of inodes etc.

Task: Install Filesys::DiskSpace

First you need to install this perl module using apt-get or from cpan (Comprehensive Perl Archive Network).
$ sudo apt-get install libfilesys-diskspace-perl

Perl script code to monitor disk space

Now write a perl script called df.pl:
$ vi df.pl
Append following code:

#!/usr/bin/perl
use strict;
use warnings;
use Filesys::DiskSpace;
 
# file system /home or /dev/sda5
my $dir = "/home";
 
# get data for /home fs
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
 
# calculate free space in %
my $df_free = (($avail) / ($avail+$used)) * 100.0;
 
# display message
my $out = sprintf("Disk space on $dir == %0.2f\n",$df_free);
print $out;
 

Save and close the file. Run this script as follows:
$ chmod +x df.pl
$ ./df.pl

Output:

Disk space on /home == 75.35

So /home has 75.35% free disk space. Next logical step is to compare this number to limit so that you can send an email if only 10% free disk space is left on /home file system. Here is the code with

#!/usr/bin/perl
use strict;
use warnings;
use Filesys::DiskSpace;
 
my $dir = "/home";
 
# warning level 10%
my $warning_level=10;
 
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
my $df_free = (($avail) / ($avail+$used)) * 100.0;
 
# compare free disk space with warning level 
if ($df_free < $warning_level) {
my $out = sprintf("Send an Email - Disk space on $dir => %0.2f%% (WARNING Low Disk Space)\n",$df_free);
print $out;
}
else
{
my $out = sprintf("Disk space on $dir => %0.2f%% (OK)\n",$df_free);
print $out;
}

Run script as follows:
$ ./df.pl
Output:

Send an Email - Disk space on /home => 3.99% (WARNING Low Disk Space)

Here is final code that send an email alert ( download):

#!/usr/bin/perl
# Available under BSD License. See url for more info:
# http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html
use strict;
use warnings;
use Filesys::DiskSpace;
 
# file system to monitor
my $dir = "/home";
 
# warning level
my $warning_level=10;
 
# email setup
my $to='admin@yourdomain.com';
my $from='webmaster@YOURDOMAIN.COM';
my $subject='Low Disk Space';
 
# get df
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
 
# calculate 
my $df_free = (($avail) / ($avail+$used)) * 100.0;
 
# compare 
if ($df_free < $warning_level) {
my $out = sprintf("WARNING Low Disk Space on $dir : %0.2f%% ()\n",$df_free);
 
# send email using UNIX/Linux sendmail
open(MAIL, "|/usr/sbin/sendmail -t");
 
## Mail Header
print MAIL "To: $to\\n";
print MAIL "From: $from\\n";
print MAIL "Subject: $subject\\n";
 
## Mail Body
print MAIL $out;
 
close(MAIL);
}
 

You can run this script as a cron job:
@hourly /path/to/df.pl

Recommended readings

=> Read man page of this module by typing following command:
$ man filesys::diskspace

=> CPAN filesys::diskspace webpage

=> Sending mail with Perl mail script and How do I send html email from Perl?

=> Shell script to monitor or watch the disk space

Lighttpd Setup CGI-BIG CGI For Perl Programs

Lighttpd logo

Lighttpd has mod_cgi module that allows you running Perl and other server side programs via cgi-bin directory. The Common Gateway Interface (CGI) is a standard protocol for interfacing external application software with an information server.

Step #1: Create a CGI cgi-bin Directory

First you need to create a cgi-bin directory for your domain. Assuming that your domain hosted at /home/lighttpd/theos.in/http (DocumentRoot), create cgi-bin as follows:
# mkdir -p /home/lighttpd/theos.in/cgi-bin

Step # 2: Load mod_cgi Module

Open lighttpd configuration file using a text editor such as vi:
# vi /etc/lighttpd/ligttpd.conf

Now append or modify text as follows so that support for mod_cgi get loaded:

server.modules += ( "mod_cgi" )

Find out your virtual server configuration and append the following:

$HTTP["url"] =~ "/cgi-bin/" {
      cgi.assign = ( ".pl" => "/usr/bin/perl" )
}

Here is complete my config code:

$HTTP["host"]  =~ "theos.in" {
  server.document-root = "/home/lighttpd/theos.in/http"
  accesslog.filename         = "/var/log/lighttpd/theos.in/access.log"
  $HTTP["url"] =~ "/cgi-bin/" {
      cgi.assign = ( ".pl" => "/usr/bin/perl" )
  }
}

Step # 3:Restart the Lighttpd

Restart lighttpd webserver, enter:
# /etc/init.d/lighttpd restart

Step # 4: Test It

Create a file/perl program in /home/lighttpd/theos.in/cgi-bin/sample.pl:

#!/usr/bin/perl
print "Content-Type: text/plain", "\n\n";
print "Hi there! This is a sample perl program!!!", "\n";

Save and execute the program (http://yourdomain.com/cgi-bin/sample.pl).

HTTP 500 error when I run my Perl script

At my work place, we provide shared hosting and http 500 error is the most common in trouble tickets. Although everything is properly configured we still get reports of error.

Send MIME type

99% time - it was not servers fault at all. I found that the most common reason for HTTP 500 errors is not printing the MIME type before outputting data to the web browser. All you need following line before outputting data to the web browser:

print "Content-type: text/html\n\n";

Perl does not automatically generate HTML. Also try following few suggestions:

Permissions

You need to setup file permissions on perl script

chmod  755 perlscript.pl
chmod  +x perlscript.pl

Perl script location
It must be in cgi-bin directory (or directory configured to run perl). Some servers are configured to run your CGI scripts anywhere.

Error log file

Please check your log file to find errors in your Perl script.

tail -f /var/log/httpd/error.log
tail -f /var/log/httpd/access.log

Debug

Pass -wc option to perl script at shell prompt, enter:

perl -wc myscript.pl

It will often point out exactly where the trouble is.

I am not perl guru but as a sys admin, I need to look after this kind of problems. What other techniques all of you follow to troubleshoot Perl http 500 error problem?