Friday, February 28, 2014

Hello World!

Ok, now we are going to make the traditional 'Hello World!' script.

For those unfamiliar to this programming tradition, it is basically a script, that upon execution prints out the text "Hello World!" on your screen.


Now, here is the explanation for the above code:

Line 1:  
#!<some path> ----> this is required for your script to work, it tells where the perl files are located.  #!/usr/bin/perl is saying that perl files are located in a local folder, which is located at /user/bin/perl  

The above is correct for a Linux system, where Perl is installed in its default location. 

While -T is for taint mode (a security feature) and w is for warnings.

Line 2: use strict is a security feature, and is strongly recommended!

Line 4: This line is very very important, and you would need to use it in all pages generally. It says, that after this whatever is accompanied with a print command, is shown on the monitor.

Line 5: This is the first print command, that would be displayed on screen. Try typing anything instead of the 'Hello World!' text. The \n is for newline after the text.. similar to what we use in C++

The last thing to do now, is to copy the above code, into one of your favorite text editor like Notepad++ and save the file as test.pl

Then put it into the cgi-bin folder, on your website and set its permissions as 600 or 700 or 705. To test it, set its permission as 700, and then go to:  www.yourwebsite.com/cgi-bin/test.pl

You should see a Hello World! written on the top-left side.

Note: You might need to add the following line for shared hosting accounts, like of Godaddy, since they have cpanel installed, make this your Line No.2 in that case:

use cPanelUserConfig; #needed for shared hosting having cpanel like Godaddy


Basic Notes about Perl/CGI

Before we jump to our first "Hello World!" script. Lets get some basics up and ready.

Things You Should Know About Perl


- It is used by major websites like the BBC.co.uk, Craiglist, DuckDuckGo, Amazon, ImDB, LiveJournal and projects like the Cpanel, Slashdot, Bugzilla, etc. So yeah, its not as obscure as one would initially think.

- Perl is often said to be difficult, but that might be because it is a "Jack of all Trades". It requires a bit more patient approach to learn. So the rule to remember, when learning Perl is "Stay in the Ice".

- You can find perl scripts to accomplish almost everything on
http://www.cpan.org
https://www.metacpan.org

But here is the catch: Explanations are a bit hard to follow. And there is no attached forums, to discuss the modules. When you get stuck, try to Google a solution or ask a question on:

http://www.Perlmonks.org
https://www.stackoverflow.com


Things You Should Know about CGI


- CGI is old, slow and inefficient for most of the stuff. Around 10x times slower than its modern avatar FCGI. 


-Most of the time people will tell you how CGI is outdated/slow/crap/etc, and would tell you about one of the Web Frameworks of Perl, like Mojolicious, Dancer, Catalyst etc. Well listen to them, if you want to take the easy road, these frameworks really are a 'job well done' and makes a lot of things easy for you. 

But if you want to take the hard way(slightly), its better to learn the very roots on which these frameworks are built.

- Also, on various Shared Hosting (like Godaddy.com) you would find a folder called 'cgi-bin'. You would have to put all of your .cgi/.pl/.fpl scripts into this folder in order to run them. With Permissions 600 or 700 or 705 in order to run them.