Previous
Legal 
Next
Hello World 
Beginner's Tutorial for CGI using Perl Language
Basic Rules and Syntax

Basic Rules and Syntax

The most basic things about perl are...

1) The #!/usr/local/bin/perl line. Put this in the top of the file. Line No 1. What does it do? This tells the system where the perl interpreter is located. Now there is a problem here. Different systems have perl at different locations. So you have to find where the perl executable is located in your system. Sometimes it will be #!/usr/bin/perl. Now if you are using perl in Windows, the script will run without this line. But put it in anyway. Make it a habit.

2) The second thing is the semi-colon or ;. Put it at the end of every statement. For example...
print "Hello World";
If you have any experience with C++, C etc, this will be familiar to you.

3) Include as many comments as possible. One of the most noted disadvantages of perl is its bad readability. So if you want to understand what you wrote today when you read it tomorrow, you'd better put comments wherever you can. Comments can be included with the '#' character. Rather harsh, don't you think? Sorry about the pun - couldn't resist. This is the example of a comment...

#This is a comment
print "Hell World"; # Everything after the '#' is a comment

4) Double quotes or single quotes may be used around strings:

print "Hello, word";
print 'Helo world';

However, only double quotes will substitute variables and special characters such as new lines (\n):

print "Hello, $name\n"; # works fine
print 'Hello, $name\n'; # prints $name\n literally

5) You can use parentheses(brackets - ')') for functions' arguments or omit them according to your personal taste. They are only required occasionally to clarify issues of precedence.

print("Hello, world\n");
print "Hello, world\n";

6) Variables are words which has the '$', '@' or '%' symbol at the start. They need not be declared. But if you want a local scope variable use the keyword 'my'. You can learn more about variables later.

Previous
Legal 
Next
Hello World 
Subscribe to Feed