Previous
Basic Rules and Syntax 
Next
Variables, Arrays and Hashes 
Beginner's Tutorial for CGI using Perl Language
Hello World

Hello World

Now let us move to the real part - the programming part. Ready for the "Hello World" program?

#!/usr/local/bin/perl
print "Hello World";

Type this out in a text editor like notepad and save this to a file called 'hello.pl'. If you are using Linux/Unix, run the command 'perl hello.pl'. If you are using Windows and you have installed ActivePerl, just double click the file and the script will be executed by the interpreter.

OK. Lets see what the program does line by line. Most of the things I have already said in the Basics section.

Line 1 : #!/usr/local/bin/perl
This line tells the system where the perl interpreter is located. But this could be different in different systems. See where your perl interpreter is located by running the 'type perl' or 'which perl' command. This is not necessary in windows. But if you plan to make a CGI script, this line is a must if it is hosted on a Unix based server. And make sure that the first line is pointed to the correct location.

Line 2 : print "Hello World";
If you don't know what this line means, bang your head on the keyboard three times. If you do know what this line means, I will reinforce your knowledge by telling you what it does. This will Print the string "Hello World". Notice the semi-colon(;) at the end of the line? Let me tell you the rule number one of Perl : Always put the semi-colon at the end of the line. If you don't, the perl interpreter will punish you with a very strongly worded error message.

The second line shows how to output stuff. But what if we want to get user input?
$var = <STDIN>;
This statement will get data from "Standard Input"(Keyboard) and store the data in a variable called '$var'. Please note that the STDIN must be in capital letters.

Great! Now you are a perl programmer. But you are not a CGI Joe yet. Don't know what a CGI Joe is? A CGI Joe is a hard-core CGI script programmer with all the social skills and charisma of a plastic action figure.

Previous
Basic Rules and Syntax 
Next
Variables, Arrays and Hashes 

Comments

Clark at 19 Apr, 2007 10:56
Any idea on windows how to get the command prompt window to stay open and display the "Hello World" long enough to see it? For me it flashes up and goes away too quickly.
Reply to this.
Binny V A at 19 Apr, 2007 08:14
Well, you could use the sleep function. But I think a better way is to got to folder holding the script using a DOS terminal and executing the command perl SCRIPT_NAME
Reply to this.
Anonymous at 02 Jun, 2008 12:40
I m writing this code but can not have idea how to give input for the same.

#!/usr/bin/perl
print("Enter a no");
$a=<STDIN>;

please help

--
Thank You
Reply to this.
Anonymous at 17 Jun, 2008 05:34
use this.

#!/usr/bin/perl
print("Enter a no ");
chomp ($a = <STDIN>);
print($a);

<STDIN>
-------------------

the above will print "Enter a no " you put in a number, get rid of the extra newline(what's produced whne you press ENTER)
it will print the variable contents to the screen.

After that it will wait for you to press something again (the second <STDIN>) before it actually closes.
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The Nexty form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed