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!

Tuesday, July 22, 2008

Objective-C for iPhone application development


iPhone is getting popular day-by-day. Naturally the demand for iPhone applications is increasing. So if you are ready to take the next big step, you can start learning how to develop software for iPhone.

The operating system used in iPhone is called iPhone OS (which is also the operating system for iPod). Unfortunately iPhone doesn't support Java like most of the smart phones. The programming language that can be used to write programs for iPhone is Objective-C. If you already know it, then you are one step ahead. Else you can start learning it. You can read this tutorial: http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/ which is recommended by many.

For resources, you must visit iPhone Developer's Center: http://developer.apple.com/iphone/ and download the iPhone SDK.

Please share your experience with iPhone application development.

Wikipedia links: iPhone, Objective-C, iPhone OS

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.