Perl

Perl

Perl Perl (sometimes backronymed to Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister) is a programming language created by Larry Wall in 1987 that borrows features from C, sed, awk, shell scripting (sh), and (to a lesser extent) from many other programming languages as well. The name is normally capitalized (“Perl”) when referring to the language, but not capitalized (“perl”) when referring to the interpreter (e.g. “Only perl properly parses Perl.”)Rationale Perl was designed to be a practical language to extract information from text files and generate reports. One of its mottos is There is more than one way to do it (TIMTOWTDI – 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 object to this aspect Perl as not a cleanly designed language because of its multiple paradigms. Perl has a powerful regular expression support built in directly to the 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 data structures that were not designed to be tied together.Perl is free software, available under the Artistic License and GPL. Perl is available for most operating systems but is particularly prevalent on Unix and Unix-like systems (eg. Linux, FreeBSD), and is growing in popularity on Microsoft Windows systems. As an example of Perl in action, until January 2002 the software running Wikipedia was a CGI script written in Perl. 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.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. Most or all of the software on CPAN is also available under either the Artistic License, the GPL, or both. 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 the source code one line at a time. Rather, perl first compiles an entire program to an intermediate byte code (much like Java’s byte code), optimizing as it goes, and then executes that byte code. It is possible to compile a Perl program to byte code to save the compilation step on later executions, though the “interpreter” is still needed to execute that code.The current version 5.8 includes Unicode support. Perl 6 is currently being developed. It will run on Parrot, a virtual machine currently 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 ( array ) { 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) }Perl and SQL databases DBI/DBD modules can be used to access MySQL and other ANSI SQL databases.Perl 5Perl5 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 poly-adic; any number of arguments can be passed to any Perl subroutine, in general.Perl 6Perl6 will separate parsing and compilation and runtime, making the virtual machine more attractive to developers looking to port other languages to the architecture.Parrot is the Perl6 runtime, and can be programmed at a low level in Parrot assembly language. Parrot exists in a limited form as of June, 2003, and a small number of languages (Jako, Cola, Basic, Forth and a subset of Perl 6) exist simply to be ‘compiled’ down to Parrot assembly language opcodes.Code samplesSome people humorously claim Perl stands for ‘Pathologically Eclectic Rubbish Lister’ due to the high use of meaningful punctuation characters in the language syntax, as may be seen in this example program to print a greeting: # A sample Perl program $_ = “Hello, world! The magic number is 234542354.n”; print; s/d+/-1/; print;and its output: Hello, world! The magic number is 234542354. Hello, world! The magic number is -1.The fourth line of the example shows the use of a regular expression.Regular Expressions with Perl ExamplesRegular ExpressionDescriptionExampleNote that all the if statements return a TRUE value.Matches an arbitrary character, but not a newline.$string1 = “Hello Worldn”;if ($string1 =~ m/…../) { print “$string1 has length >= 5n”;} ( )Groups a series of pattern elements to a single element. When you match a pattern within parentheses, you can use any of $1, $2, … $9 later to refer to the previously matched pattern.Program:$string1 = “Hello Worldn”;if ($string1 =~ m/(H..).(o..)/) { print “We matched ‘$1’ and ‘$2’n”;}Output:We matched ‘Hel’ and ‘o W’;+Matches the preceding pattern element one or more times.$string1 = “Hello Worldn”;if ($string1 =~ m/l+/) { print “There are one or more consecutive l’s in $string1n”;}?Matches zero or one times.$string1 = “Hello Worldn”;if ($string1 =~ m/H.?e/) { print “There is an ‘H’ and a ‘e’ seperated by “; print “0-1 characters (Ex: He Hoe)n”;} ?Matches the *, +, or {M,N}’d regexp that comes beforeas few times as possible.$string1 = “Hello Worldn”;if ($string1 =~ m/(l+?o)/) { print “The non-greedy match with one or more ‘l’ print “followed by an ‘o’ is ‘lo’, not ‘llo’.n”;} *Matches zero or more times.$string1 = “Hello Worldn”;if ($string =~ m/el*o/) { print “There is a ‘e’ followed by zero to many”; print “‘l’ followed by ‘o’ (eo, elo, ello, elllo)n”;} {M,N}Denotes the minimum M and the maximum N match count.$string1 = “Hello Worldn”;if ($string1 =~ m/l{1,2}/) { print “There exists a substring with at least 1”; print “and at most 2 l’s in $string1n”;} […]Denotes a set of possible matches.$string1 = “Hello Worldn”;if ($string1 =~ m/[aeiou]+/) { print “$string1 contains a one or more”; print “vowelsn”;} |Matches one of the left or right operand.$string1 = “Hello Worldn”;if ($string1 =~ m/(Hello|Hi)/) { print “Hello or Hi is “; print “contained in $string1”;} bMatches a word boundary.$string1 = “Hello Worldn”;if ($string1 =~ m/bllob/) { print “There is a word that starts with”; print ” ‘llo’n”;} else { print “There are no words that start with”; print “‘llo’n”;} wMatches alphanumeric, including “_”.$string1 = “Hello Worldn”;if ($string1 =~ m/w/) { print “There is at least one alpha-“; print “numeric char in $string1 (A-Z, a-z, 0-9, _)n”;} WMatches a non-alphanumeric character.$string1 = “Hello Worldn”;if ($string1 =~ m/W/) { print “The space between Hello and “; print “World is not alphanumericn”;}sMatches a whitespace character (space, tab, newline, formfeed)$string1 = “Hello Worldn”;if ($string1 =~ m/s.*s/) { print “There are TWO whitespace “; print “characters seperated by other characters in $string1”;} SMatches anything BUT a whitespace.$string1 = “Hello Worldn”;if ($string1 =~ m/S.*S/) { print “There are TWO non-whitespace “; print “characters seperated by other characters in $string1”;} dMatches a digit, same as [0-9].$string1 = “99 bottles of beer on the wall.”;if ($string1 =~ m/(d+)/) { print “$1 is the first number in ‘$string1’n”;}Output:99 is the first number in ’99 bottles of beer on the wall.’ DMatches a non-digit.$string1 = “Hello Worldn”;if ($string1 =~ m/D/) { print “There is at least one character in $string1”; print “that is not a string.n”;} ^Matches the beginning of a line or string.$string1 = “Hello Worldn”;if ($string1 =~ m/^He/) { print “$string1 starts with the characters ‘He’n”;} $Matches the end of a line or string.$string1 = “Hello Worldn”;if ($string1 =~ m/rld$/) { print “$string1 is a line or string”; print “that ends with ‘rld’n”;}In common with C, obfuscated code competitions are an interesting feature of the Perl culture.Similar to http://www.perlmonks.org/index.pl?node=Perl%20Poetry.External Links dmoz on Perl (http://dmoz.org/Computers/Programming/Languages/Perl/) Perl.org (http://www.perl.org) Perl Monger user group site (http://www.pm.org) The Perl Monastery (http://www.perlmonks.org) ActiveState – Perl for Microsoft Windows|Windows platforms (http://activestate.com) Comprehensive Perl Archive Network (http://www.cpan.org/) Search the Comprehensive Perl Archive Network (http://search.cpan.org/) Perl 6 development (http://dev.perl.org/perl6/) Parrot virtual machine (http://www.parrotcode.org/) – Perl POD documentation (http://www.perldoc.com/)This article is licensed under the GNU Free Documentation License, and uses material from the Wikipedia article “perl”.

Related Search :

unlimited web hosting,small business web host,web hosting packages,anonymous web hosting,best web hosts for blogs,online web hosting,web hosting for wordpress,best web host for photographers
best web hosting reviews,,free ftp web hosting,fatcow web hosting,web design and hosting services,enom web hosting,who has the best web hosting service,how to use amazon ec2 for web hosting