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:

Send email using PHP

Sending emails using a PHP script / program is very easy. Just use the mail function: mail($to, $subject, $body);

Check mail function for details.

There can be some problems if it's not configured properly. Check the installation / configuration page 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!