Sometimes, it's said that perl is the glue that holds something vital together. And it really is glue: you can pound out a quick little script to do little administrative tasks (not that we have a need to on Windows: many scripts under Linux are written in perl, though). It is also famous for it's tendency to look like swearing, especially regexs. When you keep in mind the fact that perl just grew from being a Practical Extraction and Report Language, you can conclude that it's... well, still growing up. But enough talk. Let's use perl to do some cgi.
Common gateway interface. Who knows who came up with the name. It's just another way of saying scripting for the web, or web servers, which is a whole lot shorter than saying it all out, now that I think about it.
Well, since we're under Windows, we should use ActiveState's perl, which can be gotten here.
Perl is an imperative, loosely object oriented, loosely typed, and dynamically typed language.
And here's some code:
p>#!/perl/bin/perluse strict;use warning;use CGI;my $cgi = new CGI;
print $cgi->head();
my $body = "";
my $head = tag "head", tag("title", "Greetings, Earthlings"); my $body .= $head . tag "body", tag("h1", "A header!") . tag("p", "do you want to go to " . tag("a", "google", "href,google.com") . "?");
#final print will print out everything: print tag "html", $body,"";
sub tag { my $name = pop @_; my $body = pop @_; my $attributes = pop @_; my $final = "<".name; if($attributes == "" $attributes == null) { my @attr = split /;/, $attributes; $attributes = ""; foreach(@attr) { my @temp = split /,/, $_; $attributes .= " " . temp[0] . "=\"" . temp[1] . "\""; } $final .= $attributes; } if($body == "") { $final .= "/>\n"; } else { $final .= ">" . $body . "<" .$name."/>"; } return $final;}
This is just a little, utterly useless example that displays this website. It would have been much better to use a static page for this trivial example, but that's just that: a trivial example to get you aquainted with the language. Now, a quick overview of the quirks of perl:
The use keyword is synonymous with the import in java, which finds a module and uses it1
You might have noticed that variables take on various ways of being declared, including the my in front of every declaration, and the $ in front of some and the @ in front of others. The my, in contrast to our, tells perl that the variable is local and not global. It's not necessary, but it can sometimes prevent errors and help with debugging.
The $ and @ are just two types of variables in perl, inluding a third, %. The variables prefixed with $ are scalars, while @ denotes arrays, and % stands for something that is only an extension in other languages: a hash.
Hashes are
There aren't any! Instead, perl calls them subs, and they don't have parameter lists. Instead, perl creates an implicit array, @_, and puts all the parameters there. You can then extract the list's items by using the subroutine pop on the @_, treating the array as a fifo list.
Loop - foreach
Regex
The oop syntax in perl is somewhat weird: instead of a . operator, perl uses ->. And, perl uses it's package system to define oop behavior.