Hello World and Beyound


Now, it is time for the hello world program. In this session, I will show you how to display a message both using a perl script, and then in CGI, then we will repeat the process dealing with Arabic, and finally, we will open the text file of the Quran and do some basic operations.

The First Message

Here is the simplest greetings from perl:
     1  #!/usr/bin/perl
     2  print "Peace be upon you \t";
     3  print "السلام عليكم \n";

line 1: the hash # is for comments, but in this case it has special purpose to let compiler know the path where 'perl' is available.
line 2: prints this message followed by a tab \t.
line 3: prints another message in Arabic followed by a new line \n.
And here is the output, note that on the shell, you print perl program name, in my case, the program is hello.pl.
[tutorial]>perl hello.pl
Peace be upon you       السلام عليكم

One thing I have be quit about -and I better keep quit- is the rendering of Arabic letters. Well, for displaying it in the shell prompt, all I deed is to configure my PuTTy session in a way that displays unicode utf8 characters, you might have other ways and let me know!

CGI Greeting

Here goes a CGI scripts that displays the greeting "peace be upon you" in the browser.
     1  #!/usr/bin/perl -wT
     2  use CGI qw(:standard);
     3  use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
     4  print header;
     5  print start_html("Text Mining the Qur'an");
     6  print "peace be upon you";
     7  print end_html;
line 2: This is a handy package that helps create html headers and footers, without going into details just blindly use it!
line 3: This sends error message in your perl code to the browser so you know what went wrong!
line 4,5: This is a shortcut that prepares the header and title of the html file, thus it prints things like the following to the head of your file:
<!DOCTYPE html
	PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Text Mining the Qur'an</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /></head>
<body>
line 6: this is the only thing printed to the browser!
line 7: closing the html file with things like </body></html>.

I named the file as hello.cgi then I set the permission of this file so others can execute it through chmod 755 hello.cgi. Click here to see the greetings.

CGI and Arabic Greetings

Let us do the same but add an Arabic greeting, here is the file listing:
     1  #!/usr/bin/perl -wT
     2  use CGI qw(:standard);
     3  use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
     4  print header(-type => "text/html", -charset => "utf-8");
     5  print start_html(-encoding=>'utf-8',-title=>"quran");
     6  print "peace be upon you<br>";
     7  print "السلام عليكم";
     8  print end_html;
The only important note here is at line 4 and 5 related to utf-8 and encoding settings of the html file. Note also in line 6, we needed the html tag <br> to go to the next line and the perl \n would not work. Line 7 appears wired but do not woryy the arabic words should be enclosed within double quotes and semicolon follows, this is only a rendering problem. Here is the output arabichello.cgi.

File Handling

Following listing, open's the file saheeh.tab which holds the tafsir and displays the first five lines of the file.
     1  #!/usr/bin/perl
     2  open(FH, 'saheeh.tab') || die('can not open file');
     3  for ($i=0;$i<5;$i++)
     4  {
     5   $line= <FH>;
     6   print "$line";
     7  }
Line 2: opens a file for reading, and prompts for error if opening failed.
Line 3: Looping 5 times.
Line 5: read one line of the file, and store that line in a variable called $line .
Here is the result.
[data]>perl display.pl
1|1|In the name of Allah , the Entirely Merciful, the Especially Merciful.
1|2|[All] praise is [due] to Allah , Lord of the worlds -
1|3|The Entirely Merciful, the Especially Merciful,
1|4|Sovereign of the Day of Recompense.
1|5|It is You we worship and You we ask for help.
Let now do the same but using CGI and the Arabic Quran. Here is the script.
 1  #!/usr/bin/perl -wT
     2  use CGI qw(:standard);
     3  use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
     4  open(FH, 'tanzil-verses.txt') || die("could not open file");
     5  print header(-charset => "utf-8");
     6  print start_html(-encoding=>'utf-8',dir=>'rtl', -title=>"Text Mining the Qur'an");
     7  for ($i=0;$i<5;$i++)
     8  {
     9   $line= <FH>;
    10   print "$line<br>";
    11  }
    12  close(FH);
    13  print end_html;
The logic in this CGI is the same as the perl counterpart. Here is the output in first.cgi.

Towards the end of this session, you learned how to greet yourself by a perl script, and how to geet the world also using a CGI script. You know how to do that in Arabic as well. And also you know the basic file reading, and looping in perl. Now, I will take you through an interesting file handling excersie.


<< What you need | Start | More File Handling >>
tutorial@textminingthequran.com