
A little introduction to Perl
Long, Long ago, in a city Far, Far away, Larry Wall invented Practical Extraction and Report Language; otherwise known as Perl. It is now used as the top language used for gateway scripts on the web.
I think the main reason Perl is so popular, is that it is so simple.
#!/usr/bin/perl
print "Wasn't that Easy?\n";
What you see above is actually a working Perl Program. Those two lines of code can actually do something. Let's try breaking it apart, part by part.
The first line of every Perl program is:
#!/usr/bin/perl
(well, there are some exceptions)
That line informs the script, where on the server the Perl interpreter actually is.
The next line:
print "Wasn't that Easy?\n";
is what the perl program does. "print" is the standard output function in perl. Perl is not like HTML or Basic where the output is the only thing not coded.
Wasn't that Easy
The previous sentence be the thing that would show up on your screen.
So, that means that Perl will "print" everything that is in the quotes.
\n
"/n" basically tells perl to start a new line .
Semicolons at the end of perl programs are usually optional, but with many lined programs (programs that do more than one thing), they separate different functions.
Here is an example of a working CGI program:
This program will intake your name and output it with extra stuff.
#!/usr/bin/perl
print "Hi, how are you?\n";
print "What is your name\n";
$name = <STDIN>;
chop ($name);
print "Well $name, its good to see you!\n"
Dealing with this one, will be a little bit harder.
We have already established the first three lines of code so let's move on to the fourth.
This is the line where the magic occurs.
$name
is what is called a scalar valariable. Scalars in Perl hold a single piece of information (anything from the number 1 to the words "Advanced Networks")
$
tells Perl that "name" will hold one piece of information.
<STDIN>
simply means Standard Input. This tells Perl that the user is going to input a piece of information. That piece of information will then be stored in the scalar:
$name.
Its rather logical: $name equals whatever the user inputs. In this case the user will input her/his name. So, if I was running this program, I would input Wrug Ved. So, $name would then equal: Wrug Ved.
Okay...the chop:
($name) is an operation. Chop simply takes the last character in a statement and chops it off. In this case it chops of the: \n (from the "What is your name" statement). The last line: print "Well $name, its good to see you!\n" is the final output of the program. So, if I ran this program, that final statement would read: Well Wrug Ved, its good to see you!
See, where ever the $name shows up, it still will equal whatever you entered as its input.