Perl Programming Republic of Perl (http://www.perl.com) logo Perl, also Practical Extraction and Report Language (a backronym, see below), is a programming language released by Larry Wall on December 18, 1987 that borrows features from C, sed, awk, shell scripting (sh), and (to a lesser extent) from many other programming languages.
RationalePerl was designed to be a practical language to extract information from text files and to generate reports from that information. One of its mottoes is "There's more than one way to do it" (TMTOWTDI - pronounced 'Tim Toady'). Another is Perl: the Swiss Army Chainsaw of Programming Languages. One stated design goal is to make easy tasks easy and difficult tasks possible. Its versatility permits versions of many programming paradigms: procedural, functional, and object-oriented (though some claim that Perl is not a cleanly designed language because of its multiple paradigms). Perl has a powerful regular expression engine built directly into its syntax. Perl is often considered the archetypal scripting language and has been called the "glue that holds the web together", as it is one of the most popular CGI languages. Its function as a "glue language" can be described broadly as its ability to tie together different systems and interfaces that were not designed to interoperate. Perl is one of the programming language components of the popular LAMP free software platform for web development. Perl is free software, available under a combination of the Artistic License and the GPL. It is available for most operating systems but is particularly prevalent on Unix and Unix-like systems (such as Linux and FreeBSD), and is growing in popularity on Microsoft Windows systems. As an example of Perl in action, AskFactMaster.Com itself was a CGI script written in Perl until January 2002. Another example is Slashdot, which runs on the Perl-based Slashcode software. When used on the web, Perl is often used in conjunction with the Apache web server and its mod_perl module. Perl is regarded by both its proponents and detractors as something of a grab bag of features and syntax. The difference between the two camps lies in whether this is seen as a virtue or a vice. Perl votaries maintain that this varied heritage is what makes the language so useful. Reference is often made to natural languages such as English and to evolution. For example, Larry Wall has argued that:
In recognition of its ugly-but-useful nature, Perl has adopted the camel as its mascot; and the O'Reilly manual on Perl, Programming Perl, is known as the camel book: so named because of the camel that graces its cover. ImplementationA huge collection of freely usable perl modules, ranging from advanced mathematics to database connectivity, networking and more, can be downloaded from a network of sites called CPAN, an acronym for Comprehensive Perl Archive Network. Most or all of the software on CPAN is also available under either the Artistic License, the GPL, or both. As of 2004, CPAN includes more than 7,000 modules, contributed by near 4,000 authors. CPAN.pm is also the name of the Perl module that downloads and installs other Perl modules from one of the CPAN mirror sites: such installations can be done with interactive prompts, or can be fully automated. Although Perl has most of the ease-of-use features of an interpreted language, it does not strictly interpret and execute source code one line at a time. Rather, perl (the program) first compiles an entire program into an internal form (a parse tree) which is then optimized before being run. This produces a number of differences from traditional interpreters. Any syntax errors are caught during the compile stage instead of later during execution. Subroutines calls can be placed in the file before the subroutines themselves are defined. And long-running programs are rather fast and efficient compared to strictly-interpreted languages, at the expense of short programs suffering the overhead of the compile-optimize stage. Since version 5.005 it has been possible to compile a Perl program to byte code to save the compilation stage on later executions, though the "interpreter" is still needed to execute that code. This could be seen as a precursor to Parrot. Current versionThe current version, 5.8.5, includes Unicode support. Development of the next major release, Perl 6, is also underway. It will run on Parrot, a virtual machine which is being developed as a possible multi-language target architecture. Control structuresThe basic control structures do not differ greatly from those used in the C or Java programming languages: Loops: while ( Boolean expression ) {
statement(s)
}
do {
statement(s)
} while ( Boolean expression );
do {
statement(s)
} until ( Boolean expression );
for ( initialisation ; termination condition ; incrementing expr ) {
statement(s)
}
foreach ( list ) {
statement(s)
}
If-then-statements: if ( Boolean expression ) {
statement(s)
}
unless ( Boolean expression ) {
statement(s)
}
if ( Boolean expression ) {
statement(s)
} else {
statement(s)
}
if ( Boolean expression ) {
statement(s)
} elsif ( Boolean expression ) {
statement(s)
}
For one-line statements, "while", "until", "if" and "unless" can also be used as follows: statement(s) while Boolean expression; statement(s) until Boolean expression; statement(s) if Boolean expression; statement(s) unless Boolean expression; SubroutinesSubroutines in Perl can be specified with the keyword Changes to elements in the Subroutines naturally return the value of the last expression evaluated, though explicit use of the An example subroutine definition and call follows:
sub cube
{
my $x = shift;
return $x * $x * $x;
}
$z = -4;
$y = cube($z);
print "$y\n";
Named parameters are often simulated by passing a hash. For example:
sub greeting
{
my %person = @_;
return "Hello, $person{first} $person{last}!\n";
}
print greeting(
first => 'Foo',
last => 'Bar'
);
Perl and SQL databasesDBI/DBD modules can be used to access most ANSI SQL databases, including MySQL, PostgreSQL and Oracle. Perl 5Perl5, the most current production version of perl, is an interpreter which processes the text of a Perl script at runtime. Thus, the debugger is invoked directly from the command line with perl -dw ScriptName.pl Argument1 ... ... Note that there is no limit to the number of arguments: Perl is polyadic; any number of arguments can be passed to any Perl subroutine, in general. This concept of "no arbitrary limits" is present in most other parts of the language as well. Perl can read a ten million byte string into a variable, if the machine has the memory for it. Perl 6Perl 6 is currently under development, and is planned to separate parsing and runtime, making a virtual machine that is more attractive to developers looking to port other languages to the architecture. Perl 6 plans to parse itself, and moreover expose its parser to the language itself. That is, a module could alter the grammar for the program that imported it. Parrot is the Perl6 runtime, and can be programmed at a low level in Parrot assembly language (PASM) or Intermediate Code (IMC or PIR, for Parrot Intermediate Representation). Parrot has existed in a limited form since December 2003. An increasing number of languages can be compiled to Parrot assembly language. Besides a subset of the planned Perl6, these include BASIC, Befunge, Brainfuck, Cola, Forth, Jako, m4, Miniperl, Ook, OpenComal, PHP, Plot, Python, Ruby, Scheme, Tcl, URM and YAL. Most of these other language implementations are currently still experimental. Perl code samplesThe canonical "hello world" program would be: #!/usr/bin/perl -w print "Hello, world!\n"; The first line is the shebang, which indicates the interpreter for Unix-like operating systems. (It is the most common, but not the only way of ensuring that the perl interpreter runs the program.) The second line prints the string 'Hello world' and a newline (like a person pressing 'Return' or 'Enter'). Some people (including Larry Wall) humorously claim that Perl stands for "Pathologically Eclectic Rubbish Lister" due to its philosophy that there should be many ways to do the same thing, its growth by accretion, and its origins in report writing. There are many other jokes, including the annual Obfuscated Perl contest, which makes an arch virtue of Perl's syntactical flexibility. The following program, which prints a greeting that is modified by a regular expression, is a mild example of this pastime: # A sample Perl program $_ = "Hello, world! The magic number is 234542354.\n"; print; s/\d+/-1/; print; Here is its output: Hello, world! The magic number is 234542354. Hello, world! The magic number is -1. Regular expressions with Perl examples
The 'm' in the above regular expressions, for example m/[^abc]/, is not required in order for perl to recognize the expression as a 'match' (cf. 'substitute': s/a/b/); /[^abc]/ could just as easily be used without the preceding 'm'. The 'm' operator can be used to alter the delimiting character; for example, m{/} may be used to enhance the legibility of patterns such as /\//. See 'perldoc perlre (http://www.perldoc.com/perl5.8.4/pod/perlre.html)' for more details. NamePerl was originally named "Pearl", after "the pearl of great price" of Matthew 13:46. Larry Wall wanted to give the language a short name with positive connotations, and claims he looked at (and rejected) every three- and four-letter word in the dictionary. He even thought of naming it after his wife Gloria. Before the language's official release, Wall discovered that there was already a programming language named Pearl, and changed the spelling of the name. Several backronyms have been suggested, including the humorous Pathologically Eclectic Rubbish Lister. Practical Extraction and Report Language has prevailed in many of today's manuals, including the official Perl man page. It is also consistent with the old name "Pearl": Practical Extraction And Report Language. The name is normally capitalized (Perl) when referring to the language, and uncapitalized (perl) when referring to the interpreter program itself. (There is a saying in the Perl community: "Nothing but perl can parse Perl.") It is not appropriate to write "PERL" as it is not an acronym. Fun with PerlIn common with C, obfuscated code competitions are a popular feature of Perl culture. Similar to obfuscated code but with a different purpose, Perl Poetry is the practice of writing poems that can actually be compiled by perl. This hobby is more or less unique to Perl, due to the large number of regular English words used in the language. New poems are regularly published in the Perl Monks site's Perl Poetry (http://www.perlmonks.org/index.pl?node=Perl%20Poetry) section. Another popular pastime is Perl golf. As with the physical sport, the objective is to reduce the number of strokes that it takes to complete a particular objective, but here "strokes" refers to keystrokes rather than swings of a golf club. A task, such as "scan an input string and return the longest palindrome that it contains", is proposed, and participants try to outdo each other by writing solutions that require fewer and fewer characters of Perl source code. Another tradition among Perl hackers is writing JAPHs, which are short obfuscated programs that print out the phrase "Just another Perl hacker,". One of the most bizarre Perl modules is Lingua::Romana::Perligata. This module translates the source code of a script that uses it from Latin into Perl, allowing the programmer to write executable programs in Latin. Perl humor
See also
External links
Books
cs:Perl da:Perl de:Perl et:Perl es:Perl eo:Perl Komputillingvo fr:Perl (langage) it:Perl ja:Perl lb:Perl lt:Perl nl:Perl pl:Perl pt:Perl fi:Perl sv:Perl zh:Perl
|
|
This article is licensed under the GNU Free Documentation License. It uses material from Wikipedia article. Browse Wikipedia for more information. |