Topic outline
Introduction
In computer programming, Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall, a linguist working as a systems administrator for NASA, in 1987, as a general purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and became widely popular among programmers. Larry Wall continues to oversee development of the core language, and its newest version, Perl 6.
Perl borrows features from other programming languages including C, shell scripting (sh), AWK, sed and Lisp. The language provides powerful text processing facilities without the arbitrary data length limits of many contemporary Unix tools, facilitating easy manipulation of text files. It is also used for graphics programming, system administration, network programming, applications that require database access and CGI programming on the Web. Perl is nicknamed “the Swiss Army chainsaw of programming languages” due to its flexibility and adaptability. Taken from Wikipedia
Factorials
Here are two different methods of calculating factorials using Perl. Note how the later has much fewer lines of code. This is something Perl is renowned for and I suspect someone with more Perl experience would be able to shorten it further.
The Long Way
#!/usr/bin/perl print "Enter Number: "; $input=<>; $s=1; $r=1; while ($s <= $input) { $r *= $s; $s++; } if($input == 0) { $r=0; } print "The result is ".$r."\n";
The Short(er) Way
#!/usr/bin/perl print "Enter Number: "; $input=<>; my $factorial = 1; $factorial *= $_ foreach 1..$input; print "The result is ".$factorial."\n";
HTML Page Reader
The following short program gets the contents of a webpage.
It will either print it to the screen or a file depending upon how it is called. The following command will scan the url indicated by -u switch to a depth indicated by -d and pipe the output into the named file. Omitting the
> pageContents.txtwill output the contents to screen../getPage.pl -u http://your.url.here -d 2 > outfile.txt
Topic 3