In any perl script, the first line is to indicate the location of perl. Typically, it looks like the following:

#!/usr/bin/perl

Recently, i create a perl script to validate the Recaptcha. However it always gives me 500 Internal Server Error. At first I thought there is something wrong with my script. So I keep checking and checking and still can’t find out the error. So i just change the script to the following simple script that just print out a simple page.

#!/usr/bin/perl
use strict;
use CGI;

my $query = new CGI;

print $query->header( "text/html" );
print < 
  Test
  

Test

END_HERE

However, it still gives me 500 Internal Server Error. In the end, it turns out that the error is because of the first line. #!/usr/bin/perl This script is created in windows and uploaded to unix server. In windows the next line is \r\n. However in linux the next line is \n. So the first line becomes #!/usr/bin/perl\r in unix. Of cause, you won’t be able to see this changes since \r is not a character that can be displayed. Thus if you directly run the script in unix, the error is “bad interpreter”. In order to solve this, you need to convert the script you generated in windows back to unix format. It can be done using UltraEdit. Under Files, there is a function called Convertion, where you can convert files between different format.
Read the rest of this entry »

Perl is a very flexible language.

my @names = [‘shanison’,  ‘stephanie’];

You may think that this syntax is wrong. Surprisingly, it is not.  We all know that @before a variable name means the variable must be an array. So is this one. The variable  “names” here is an array with only one element, which is an array reference. In order to make it clear, we can write it in this clearer ways:

my @names =( [‘shanison’,  ‘stephanie’]);

So you can see that the braces can be omitted in array definition when there is only one element in the array. So now you know that the following is also a correct perl syntax:

my @students =  “shanison”;

Now how you access the element in the array of array reference. Try the following:

my @names =( [‘shanison’,  ‘stephanie’]);
Print $names[0][0];
Print $names[0]->[0];

Both the above methods should give you the same results “Shanison”. Now try this:

my $array_ref = [1, 2, [‘a’, ‘b’, ‘c’]];
print $array_ref->[2][0];
print $array_ref->[2]->[0]

Both the above methods should give you the same results “a’. The above two examples show that if the array reference is in second dimension, you can omit ->.

Read the rest of this entry »