Showing posts with label Perl. Show all posts
Showing posts with label Perl. Show all posts

Tuesday, August 12, 2008

Convert FLV to MP3 using Perl

Few days back, I found an interesting Perl module that converts FLV file to MP3. Here is the code:

use FLV::ToMP3;

$flv_filename = "/home/video/music.flv";
$mp3_filename = "test.mp3";

my $converter = FLV::ToMP3->new();
$converter->parse_flv($flv_filename);
$converter->save($mp3_filename);

Visit the following links for details:

Install Perl modules using cpan

Installation of Perl modules is very simple. Just use the command in the terminal:
cpan -install ModuleName

For example, to install LWP module, use the command:
cpan -install LWP

To install Mail::Sendmail module, use the command:
cpan -install Mail::Sendmail

Sunday, August 10, 2008

Perl - return value of a function

Can you guess the output of the following Perl code?

$sum = add(3, 5);
print $sum."\n";

sub add
{
$a = shift;
$b = shift;

return ($a + $b);
}

Yes, it prints 8.

Now what about the following code?

$sum = add(3, 5);
print $sum."\n";

sub add
{
$a = shift;
$b = shift;
}

hmm... what should it print? It prints 5 ! A garbage value?
Not actually. I have found that in Perl if you don't return anything, it returns the value of the last variable in the function.

Now you can tell the output of the following code :-)

$sum = add(3, 5);
print $sum."\n";

sub add
{
$a = shift;
$b = shift;

$c = 10;
}

yes, it's 10 !

Friday, August 8, 2008

Count number of elements in array - PHP, Perl, Python

We often need to count the number of elements in an array (or get the size of an array). Here is how to do it in PHP, Perl & Python:

PHP: $items = count($ara);

Perl: my $items = scalar(@ara);, you can also try: my $items = @ara;

Python: items = ara.__len__(). Note that in Python it's called list. List can be used as array.

Sunday, August 3, 2008

CGItemp**** issue in file upload

Sometimes your file upload script produces temp files named CGItemp**** in the server (this happed to one of my Perl file upload script (cgi) running in a windows server).

So I did some search in the Internet about the problem. Many people faced the same problem and most of them choose a simple solution. That is to delete those files using a program or manually. After spending some more time, I found that those temp files are filehandles that are being recorded in the server. Though the CGI.pm file should have taken care of it (i.e. delete those files automatically).

I found that in my code I have opened a file handle which must be closed in order to get rid of this problem.

my $upload_file_handle = $q->upload("file_name");

when this variable (file handle) is not required any more, it should be closed.

So I added the line at the end of upload function:

close $upload_file_handle;

It solved the problem!

Wednesday, July 9, 2008

Create PDF file in Perl

Today I tried to create a PDF file using a program. I choose the language Perl to do this task, because there should be one or more useful module for pdf creation in Perl (I shall try to do the same task using PHP and Python later).

For PDF creation I found the module PDF::Create very useful. If you want to create and/or modify a pdf file, then you can try PDF::API2.

Here is an article on pdf creation from the book PDF Hack, that uses PDF::API2 to create a pdf file: Use Perl to create PDF. Note that I had to change the sample code (line 11) my $txt = $page->hybrid; to my $txt = $page->text;

Hope you can create PDFs smoothly. Please share your experience.

Thursday, June 19, 2008

Perl script to display the content of a zip file

Here is a little script that shows the number of files in a zip file and the names of the files. Interesting thing is that, we don't need to unzip the file.

Here is the code

use Archive::Zip qw( :ERROR_CODES :CONSTANTS );

my $zip = Archive::Zip->new();

my $zip_file_name = 'scraper.zip';

$zip->read( $zip_file_name );

print "Number of files in the zip file: ".$zip->numberOfMembers()."\n";
print "List of files:\n";

my @members = $zip->memberNames();

#now print the name of the files
foreach (@members)
{
print $_, "\n";
}


For details, please check http://search.cpan.org/~adamk/Archive-Zip-1.23/lib/Archive/Zip.pm

Thursday, June 12, 2008

Problem uploading large file in CGI script

Today I had a problem uploading large file using the CGI upload script I have written using Perl couple of day back. I was getting the response: "413 Request entity too large". I searched Google and found out that this problem can occur for mainly two reasons: if size of the file being uploaded is greater than the value of $CGI::POST_MAX in the upload script, and if the value of the apache (web server) directive LimitRequestBody is lower than the file size. But both cases were handled. But I was still getting the 413 error. Then I found that it was my local server which prevented me to upload large file size. the 413 was coming from that machine instead of the server where the script is hosted :(

How to send custom error message in http header from cgi script

Couple of days ago, while I was writing a CGI script in Perl, it was needed to send custom error message in http response header. I would like to share with you how I have done this.

First I have decided that for any kind of error in the code (for example, file not found, insecure path etc.) I shall return status 500 with the error message. Before this decision my code looked like this:

...
my $query = new CGI;
print $query->header(-type=>'text/plain');
...

So for any error in the script, I added the line:
print $query->header(-type=>'text/plain', -status=>"500 $err_str");
But it wasn't working. The return value (status line in the response) still '200 OK'. Then I found that query header can't be set twice (once you set it, you can't change it). Then I removed the first line print $query->header(-type=>'text/plain'); and modified the code in such a way that the header is printed once. In order to return error, I wrote a function and just called it in case of any error occurs (but I made sure the header is not printed before I call it). Here is the function:

sub catch_n_die
{

my $err_str = shift;

print $query->header(-type=>'text/plain', -status=>"500 $err_str");

exit;

}

Friday, June 6, 2008

Perl script to check the number of instances of a program

To get the number of instances of a program running in your machine you can use the following Perl code:

my $program_name = 'abc.pl';
my $data = `ps ax | grep perl`;
my @ara = $data =~ /perl\s+$program_name/g;
my $count = @ara;
print "$count\n";


Sometime it might be useful for you to know at the start of a program whether it's already running. You can get the name of your current Perl script/program from $0. If the name of your Perl script is program.pl, then $0 will contain program.pl. You can try print $0;
Hope this tiny piece of code will be useful for you. :)

Wednesday, June 4, 2008

execute linux command using Perl, Python, PHP

Those who program in Linux often need to execute/run linux commands from programs/scripts.

Using python you can execute linux command is the following way:

import os
os.system('ls -lt')



If you use Perl, you can do it in the following ways:
1. system(mkdir $folder);
2. qw(mkdir $folder);
3. $temp = `mkdir $folder`; #back ticks
4. exec($command);

If you use back ticks or qw, $? will be set if any error occurs. So you can use it like this:

qw(rm $filename);
if($?) {
print STDERR "error deleting file\n";
}


There are also several options available to run linux commands using PHP. Take a look at here for detals: http://us2.php.net/manual/en/ref.exec.php

Friday, March 28, 2008

Perl equivalent of UNIX / Linux commands

One afternoon I was asked by my boss to write a Perl script that does the same task as the 'egrep' command of Linux. I studied the manual, to understand what 'egrep' does and planned a solution. But next morning when I came to office, I searched google and found a the Perl equivalent of 'egrep' command. It saved me at least two hours. You can click this link for details.