Pearl
Perl is a programming language designed by Larry Wall in 1987. Perl borrows features from the C language, the bourne shell (sh) interpreted language, AWK, sed, Lisp, and to a lesser degree, from many other programming languages.
Structurally, Perl is based on a blocky style like C or AWK, and was widely adopted for its text processing prowess and not having any of the limitations of other scripting languages.
History
Larry Wall began working on Perl in 1987 while working as a programmer at Unisys and announced version 1.0 on the comp.sources.misc newsgroup on December 18, 1987. The language expanded rapidly over the next few years.. Perl 2, released in 1988, provided a better regular expression engine. Perl 3, released in 1989, added support for binary data.
Until 1991 the only documentation for Perl was a simple (and growing) Unix man page. In 1991 it was published Programming Perl (the camel's book) and became the de facto reference for the language. At the same time, Perl's version number jumped to 4, not because it marked a major change in the language, but because it identified the version that was documented in the book.
Perl 4 brought with it a series of maintenance releases, culminating in Perl 4.036 in 1993. At this point, Larry Wall abandoned Perl 4 to begin work on Perl 5. Perl 4 would remain in that version to this day.
Development of Perl 5 continued into 1994. The perl5-porters mailing list was established in May 1994 to coordinate work on porting Perl 5 to different platforms. It is the first forum for the development, maintenance, and porting of Perl 5.
Perl 5 was released on October 17, 1994. It was an almost complete rewrite of the interpreter and added many new features to the language, including objects, references, packages, and modules. Notably, modules provide a mechanism to extend the language without modifying the interpreter. This made it possible to stabilize its main kernel, as well as allowing Perl programmers to add new features to it.
Version | Update | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Old version, without technical service: 5.4 | 1999-04-29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.5 | 2004-02-23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.6 | 2003-11-15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.8 | 2008-12-14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.10 | 2009-08-22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.12 | 2012-11-10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.14 | 2013-03-10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.16 | 2013-03-11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.18 | 2014-10-02 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.20 | 2015-09-12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.22 | 2017-07-15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.24 | 2018-04-14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.26 | 2018-11-29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.28 | 2020-06-01 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.30 | 2020-06-01 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, without technical service: 5.32 | 2021-01-23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old version, with technical service: 5.34 | 2022-03-13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Current stable version: 5.36 | 2022-05-27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Last version planned for launch: 5.37 | 2022-05-27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Future launch: 7.0 | 2023? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Legend: Old Version Old version, with technical service Current version Latest version planned Future launch |
On October 26, 1995, the Comprehensive Perl Archive Network (CPAN) was created. CPAN is a collection of websites that host and distribute Perl sources, binaries, documentation, scripts and modules. Originally, each CPAN site had to be accessed through its own URL; today, http://www.cpan.org automatically redirects to one of hundreds of CPAN mirror repositories.
As of 2008, Perl 5 continues to be maintained. Important features and some essential constructs have been added, including Unicode support, threading, significant support for object-oriented programming, and other enhancements.
Name
Perl was originally named "Pearl". Larry Wall wanted to give the language a short name with positive connotations; he claims that he looked at (and rejected) all the three- and four-letter combinations in the dictionary. He also considered naming her after his wife Gloria his. Wall found out before the official release that a programming language called PEARL already existed, and changed the spelling of the name.
The name normally starts with an uppercase (Perl) when referring to the language and with a lowercase (perl) when referring to the interpreter program itself because Unix files are case sensitive. Before the release of the first edition of Programming Perl it was common to refer to the language as perl; Randal L. Schwartz, however, forced the name to be capitalized in the book so that it would stand out better when printed. The distinction was subsequently adopted by the community.
The name is occasionally described as "PERL" (for Ppractical Extraction and Report Language - Practical Language for Extraction and Report ). Although this expansion has been prevalent in many current manuals, including the Perl man page, it is a retro-acronym and the name does not officially mean anything. The uppercase spelling of PERL is therefore used as slang to detect individuals outside of the community. However, several retro-acronyms have been suggested, including the comical Pathologically Eclectic Rubbish Lister.
The Camel Symbol
Perl is generally symbolized by a one-humped camel (Arabian camel or dromedary), which was the image chosen by editor O'Reilly for the cover of Programming Perl, which by It consequently acquired the name The Camel Book. integrity and impact of this symbol". O'Reilly allows non-commercial use of the symbol, and offers Programming Republic of Perl logos and Powered by Perl buttons.
For a few years, and to avoid this type of problem with the commercial license, the Perl Foundation developed new logos based on an onion, as a result of the annual conferences that Larry Wall offers with the title: State of the Onion ("State of the Onion") Onion is pronounced very similar to Union, so it sounds similar to State of the Union ("State of the Union"), an event in which the President of the United States informs the citizens of the state in which their country is located.
Description
The Unix man page perlintro(1) says:
Perl is a general purpose language originally developed for text manipulation and is now used for a wide range of tasks including system administration, web development, network programming, GUI development and more.It was predicted to be practical (useability, efficient, complete) instead of beautiful (small, elegant, minimal). Its main features are that it is easy to use, supports both structured programming and object-oriented programming and functional programming (see Higher-Order Perl: Transforming Programs with Programs), has incorporated a powerful text processing system and a huge collection of available modules.
Features
The entire structure of Perl derives largely from the C language. Perl is an imperative language, with variables, expressions, assignments, brace-delimited blocks of code, control structures, and subroutines.
Perl also borrows features from shell programming. All variables are marked with a preceding Sigil (Sigil). Sigils uniquely identify variable names, allowing Perl to have rich syntax. Notably, sigils allow variables to be interpolated directly within character strings (string). Like shells, Perl has many built-in functions for common tasks and for accessing system resources.
Perl takes lists from Lisp, hash (associative memory) from AWK, and regular expressions from sed. All of this simplifies and facilitates all forms of parsing, text handling, and data management tasks.
In Perl 5, features were added to support complex data structures, first-order functions (i.e. closures as values), and an object-oriented programming model. These include references, packages, and class-based method execution, and the introduction of lexically scoped variables, which made it easier to write robust code (along with the strict pragma). A major feature introduced in Perl 5 was the ability to package reusable code as modules. Larry Wall later stated that "the intention of the Perl 5 module system was to support the growth of the Perl culture rather than the Perl core".
All versions of Perl do automatic data typing and memory management. The interpreter knows the type and storage requirements of each object in the program; reserve and free up space for them as needed. Legal type conversions are done automatically at runtime; illegal conversions are considered fatal errors.
Design
Perl's design can be understood as a response to three broad trends in the computer industry: lower hardware costs, increased labor costs, and improvements in compiler technology. In the past, many computer languages, such as Fortran and C, were designed to make efficient use of expensive hardware. By contrast, Perl is designed to make efficient use of expensive computer programmers.
Perl has many features that make the programmer's task easier at the cost of higher CPU and memory requirements. These include automatic memory management; dynamic data type; strings, lists and hashes; regular expressions; introspection and a eval()
function.
Larry Wall was trained as a linguist, and Perl's design has been heavily schooled in linguistic principles. Examples include Huffman Coding (most common constructs should be shortest), good layout (important information should come first), and a large collection of language primitives. Perl favors language constructs as natural as reading and writing are to humans, even if that makes the Perl interpreter more complicated.
Perl's syntax reflects the idea that "things that are different should look different". For example, scalars, arrays and hashes have different stealth. array indices and hash keys use different kinds of parentheses. Strings and regular expressions have different standard delimiters. This approach can be contrasted with languages like Lisp, where the same S-expression construct and basic syntax is used for many different purposes.
Perl has features that support a variety of programming paradigms, such as imperative, functional, and object-oriented. At the same time, Perl does not force you to follow any particular paradigm, nor does it force the programmer to choose any of them.
There is a broad sense of practicality, both in the Perl language and in the community and culture that surrounds it. The preface to Programming Perl begins with, "Perl is a language for getting your work done". One consequence of this is that Perl is not an ordered language. It includes features if people use them, tolerates exceptions to the rules, and employs heuristics to resolve syntactic ambiguities. Due to the forgiving nature of the compiler, sometimes bugs can be hard to find. Speaking of the varied behavior of built-in functions in list and scalar contexts, the man page for perlfunc(1)
says "In general, they do what you want, as long as you want the coherence."
Perl has several slogans that convey aspects of its design and usage. One is There's more than one way to do it (TMTOWTDI, usually pronounced 'Tim Toady'). Others are "Perl: The Swiss Army's Chainsaw of Programming Languages" and "Fuzzy Limits". A default goal of Perl is to make easy things easy and difficult tasks possible. Perl has also been called "The Tape of the Internet".
Applications
Perl has many and varied applications, thanks to the availability of many standard and third-party modules.
It has been used since the early days of the Web to write CGI scripts (scripts). It is one of the "three Pes" (Perl, Python and PHP), which are the most popular languages for building web applications, and is an integral component of the popular LAMP solution for web development. Big projects written in Perl are Slash, IMDb and UseModWiki, a Wiki engine. Many high-traffic websites such as Amazon.com and Ticketmaster.com use Perl extensively.
Perl is often used as a "glue language", tying together systems and interfaces that were not specifically designed to interoperate; and for "data mining," converting or processing large amounts of data for tasks such as creating reports. In fact, these strengths are closely linked. Their combination makes Perl a popular general-purpose tool for system administrators, especially small programs that can be written and executed on a single command line.
Perl is also widely used in finance and bioinformatics, where it is appreciated for its rapid development, both application and deployment, as well as its ability to handle large volumes of data.
Implementation
Perl is implemented as an interpreter, written in C, along with a large collection of modules, written in Perl and C. The source distribution is, as of 2005, 12 MB when packaged and compressed into a tar file. The interpreter is 150,000 lines of C code and compiles into a 1 MB executable on most typical hardware architectures. Alternatively, the interpreter can be compiled as a library and embedded in other programs. There are about 500 modules in the distribution, totaling 200,000 lines of Perl and an additional 350,000 lines of C code. Much of the C code in the modules consists of character encoding tables.
The interpreter has an object-oriented architecture. All the elements of the Perl language—scalars, lists, hashes, code references, file handles—are represented in the interpreter as C structures. Operations on these structures are defined as a large collection of macros, typedef and functions; this constitutes the Perl C API. The Perl API can be bewildering to the uninitiated, but its entry points follow a consistent naming scheme, which helps those who want to use it.
The execution of a Perl program can be loosely divided into two phases: compile time and run time. At compile time the interpreter parses the text of the program into a syntax tree. At runtime, it runs the program following the tree. The text is parsed only once and the syntax tree is optimized before being executed, so that the execution phase is relatively efficient. Compile-time syntax tree optimizations include constant expression simplification, context propagation, and optimization in chunks of code. However, the compile and run phases can be nested: a BEGIN
block is executed at compile time, while an eval
function starts a compile during an execution. Both operations are implicit in each other - notably, the use
clause that loads libraries, known in Perl as modules, implies a BEGIN
block.
Perl is a dynamic language and has a context-sensitive grammar that can be affected by code executed during an intermediate execution phase (See examples.). This is why Perl cannot be parsed by a direct application of Lex/Yacc parsers/parsers. Instead, the interpreter implements its own lexical analyzer, which in coordination with a modified GNU bison parser resolves language ambiguities. It has been said that "only perl can parse Perl", meaning that only the Perl interpreter (perl) can parse the Perl language (Perl). The reason for this is attested by the persistent imperfections of other programs that undertake the task of parsing Perl, such as code analyzers and auto-indenters, which have to deal not only with the many ways of expressing unambiguously syntactic constructs, but also with the fact that also Perl cannot, in general, be parsed without first being executed.
Maintenance of the Perl interpreter has, over the years, become increasingly difficult. The kernel has been in continuous development since 1994. The code has been optimized for performance at the expense of simplicity, clarity, and stronger internal interfaces. New features have been added, while still maintaining virtually full backwards compatibility with earlier versions. The size and complexity of the interpreter are a barrier for developers who want to work on it.
Perl is distributed with over 120,000 functional tests. These are run as part of the normal build process and extensively test the interpreter and its core modules. Perl developers rely on functional tests to ensure that changes to the interpreter do not introduce errors; conversely, Perl users who see the interpreter pass functional tests on their system can have a high degree of confidence that it is working properly.
There is no written specification or standard for the Perl language, and there are no plans to create one for the current version of Perl. There has always been only one implementation of the interpreter. This interpreter, together with the functional tests, form the de facto specification of the language.
Availability
Perl is free software and is licensed under the Artistic License and the GNU General Public License. There are distributions available for most operating systems. It is especially widespread on Unix and similar systems, but it has been ported to most modern platforms (and other more obsolete ones). With only six confirmed exceptions, it can be compiled from source on all Unix, POSIX-compliant, or any other Unix-compliant platform. However, this is not normally necessary, because Perl is included by default in the installation of most popular operating systems.
Due to the special changes needed to support Mac OS Classic, there is a special port called MacPerl.
GNU/Linux
Perl is pre-installed on the most popular GNU/Linux distributions including Gentoo, Slackware, Mandriva, Debian, RedHat and SUSE.
Windows
Microsoft Windows users typically install a Perl binary distribution. Compiling Perl from source under Windows is possible, but most installations do not have the necessary C compiler.
The Cygwin emulation layer provides another way to run Perl under Windows. Cygwin provides a Unix-like environment on Windows that includes gcc, so compiling Perl from code is an accessible option for users who prefer this option.
In June 2006, win32.perl.org was launched by Adam Kennedy on behalf of the Perl Foundation. It is a web community "for all things Windows and Perl".
Language structure
Sample Program
In Perl, the canonical program "Hello world" is:
#/usr/bin/perlprint "Hello world!;
The first line contains the shebang (character pair identifying the text that follows), which tells the operating system where to find the Perl interpreter. The second prints the string Hello world! and a new line character.
The shebang is the normal way to invoke the shell on Unix systems. Windows systems may continue to use it or may associate the .pl file extension with the Perl interpreter. Some text editors also use the shebang line as a hint as to the working mode in which they should operate. If the program is executed by perl and not invoked by the shell, the line beginning with the shebang is parsed to interpret the options. Otherwise, it is ignored. For details on this, see the perlrun man page.
Data Types
Perl has three data types: scalars, lists and hashes:
- A climbing is a single value; it can be a number, a string (character string) or a reference
- A list is an orderly collection of scales (a variable that stores a list is called array)
- A hash, or associative memory, is a mapping strings scales; strings It's called keys and scalers values.
All variables are preceded by a sigil, which identifies the type of data being accessed (not the data type of the variable itself). The same name can be used for variables of different types, without conflicting.
$var # a climb@var # an array%var ♪ a hash
Numbers are written in the usual way; strings are surrounded by quotes of various classes.
$n = 42;$Name = "juan";$color = 'red';
Perl will convert strings to numbers and vice versa depending on the context in which they are used. In the following example the strings $n
and $m
are treated as numbers when they are arguments to the addition operator. This code prints the number '5', discarding any non-numeric information from the operation and leaving the variable values intact. (The concatenation operator is not +
, but .
.)
$n = "3 apples";$ = "2 oranges";print $n + $;
Perl also has a boolean context that it uses in evaluating conditional statements. The following values in Perl all evaluate to false:
$false = 0; # The number zero$false = 0.0; # number zero as floating$false = '0'; # the string zero$false = "; # The empty string$false = undef; # the value returned by undef
All other values evaluate to true. This includes the curious string self-descriptive "0 but true"
, which is actually 0 as a number, but true as a Boolean. (Any non-numeric string will also have this property, but this particular string is ignored by Perl in numeric contexts.) Evaluated Boolean expressions also return scalar values. Although the documentation does not indicate which particular value is returned as true or false (and therefore unreliable), many boolean operators return 1 for true and the empty string for false. (which evaluates to zero in numerical context). The defined() function tells you if the variable has any values. In the above example defined($false) will be true with each of the above values except undef.
If, specifically, you want to ensure that you get a result of 1/0 (as in C), you need to perform the following calculation:
my $resulted_real = $resulted_booleano ? 1 : 0;
A list is defined by listing its elements, separated by commas and surrounded by parentheses where required by operator precedence.
@punctuations = (32, 45, 16, 5);
A hash can be initialized from a list of key/value pairs.
%favorite = ( ♪ = 2005 'red', sam = 2005 'blue',);
Individual elements of a list are accessed using a numerical index, enclosed in square brackets. Individual values in a hash are accessed using the corresponding key, within braces. The $ tag identifies that the accessed element is a scalar.
$punctuations[chuckles]2] # an element of @puntuations$favorito{♪! # a value of %favorite
Multiple elements can be accessed using the @ sigil instead (identifying the result as a list).
@punctuations[chuckles]2, 3, 1] # Three elements of @puntuations@favorito{'joe', 'sam'! # Two %favorite values
The number of elements in an array can be obtained by evaluating the array in scalar context or with the help of the $# stealth. The latter gives the index of the last element within the array, not the number of elements.
$numero = @amigos;$#friends; # the last element index in @amigos$#friends+1; # Normally the number of elements in @amigos, # which is one more than $#friends because the first element has the index 0, not 1
There are a few functions that operate on integer hashes.
@names_of_clients = keys % direction; # Saves on @names_of_clients all keys of %directs@directs_de_email = values % direction; # save on @directions_de_email all values of % addresses
Control Structures
Perl has several kinds of control structures.
It has block-oriented control structures, similar to those in the C and Java programming languages. Conditions are surrounded by parentheses and subordinate blocks by braces:
'Tag' while ( 'condition' ) {... ! 'Tag' while ( 'condition' ) {... ! continue {... ! 'Tag' for ( 'expression initial'; 'expression conditional'; 'expression incremental' ) {... ! 'Tag' foreach 'var' ( 'List' ) {... ! 'Tag' foreach 'var' ( 'List' ) {... ! continue {... ! if ( 'condition' ) {... ! if ( 'condition' ) {... ! else {... ! if ( 'condition' ) {... ! elsif ( 'condition' ) {... ! else {... !
When controlling a single declaration, declaration modifiers provide a lighter syntax:
'declaration' if 'condition'; 'declaration' unless 'condition'; 'declaration' while 'condition'; 'declaration' until 'condition'; 'declaration' foreach 'List';
The logical operators shortcircuit are normally used to control the flow of the program at the expression level:
'expr' and 'expr' 'expr' or 'expr'
The control flow keywords next
, last
, return
, and redo
are expressions, so that can be used with short-circuit operators.
Perl also has two implicit constructs for loops:
'results' = grep {... ! 'List' 'results' = map {... ! 'List'
grep
returns all list elements in which the subordinate block evaluates to true. map
evaluates the subordinate block for each list element and returns a list of the resulting values. These constructs allow for a simple style of functional programming.
The switch statement (called "given#34;/y#34;when") exists since version 5.10:
use 5.10.1; ♪ or later ♪ given ($variable) { when ('condition') { ... ! when ('condition') { ... ! default { ... ! !
Perl includes a goto tag
declaration, but it is rarely used. Situations where goto
is used in other languages do not occur as often in Perl due to its extensive control flow options.
There is also a goto &sub
statement that makes a 'final' call. Terminates the current subroutine and immediately calls the specified sub
. This is used in situations where a new subroutine can perform more efficient stack management than Perl itself (because typically no change to the current stack is required), and in very deep recursion such calls can have substantial Positive performance impact because it avoids context/stack management overhead at return time.
Subroutines
Subroutines are defined with the sub
keyword and invoked simply by naming them. If the subroutine in question has not yet been declared, it is necessary, for the parsing process, to put the parentheses.
Foo(); # necessary parenthesis here...sub Foo {... !Foo; # But not here
A list of arguments can be indicated after the subroutine name. The arguments can be scalars, lists, or hashes.
Foo $x, @y, %z;
The parameters of a subroutine need not be declared, neither in number nor in type; in fact, they may vary for each call. The arrays are expanded to their elements, the hashes to a list of key/value pairs, and the entire array is passed to the subroutine as an undifferentiated list of scalars.
Any of the arguments passed are available to the subroutine in the special array @_
. The @_
elements are bound to the current arguments; changing a @_
element changes the corresponding argument.
Elements in @_
can be accessed with subscripts in the normal way.
$_[chuckles]0], $_[chuckles]1]
However, the resulting code can be difficult to read and the parameters have pass-by-reference semantics, which can be undesirable.
A common idiom is to assign @_
to a list of named variables.
my($x, $y, $z) = @_;
This affects both the mnemonic of the parameter names and the semantics of the values passed by value. The my
keyword indicates that the following variables are lexically embedded in the block that contains them.
Another idiom is to get the parameters out of @_
. This is very common when the subroutine takes only one argument.
my $x = shift; # If nothing is said, we refer to @_
Subroutines can return values.
return 42, $x, @y, %z;
If the subroutine does not exit via the return
statement, then return the last evaluated expression in the body of the subroutine. Arrays and hashes in the return value are expanded to a list of scalars, just as if they were function arguments.
The returned expression is evaluated in the context of the subroutine call; this can surprise the unprepared.
sub List { (4, 5, 6) !sub array { @x = (4, 5, 6); @x !$x = List; # returns 6 - last item from the list$x = array; # returns 3 - number of items from the list@x = List; # returns (4, 5, 6)@x = array; # returns (4, 5, 6)
A subroutine can discover its calling context with the wantarray
function.
sub Anyone. { wantarray ? (1, 2) : "Naranjas" !$x = Anyone.; # returns Naranjas@x = Anyone.; # returns (1, 2)
Regular Expressions
The Perl language includes a specialized syntax for writing regular expressions, and the interpreter contains an engine for matching strings with regular expressions. The regular expression engine uses a backtracking algorithm, extending its capabilities from simple pattern matching to string capturing and substitution. The regular expression engine is derived from regex, written by Henry Spencer.
The regular expression syntax was originally borrowed from Unix Version 8 regular expressions. However, it was differentiated even before the first release of Perl and has since added many more features. Other languages and applications are adopting Perl regular expressions (PCRE) instead of POSIX regular expressions, including PHP, Ruby, Java, and Apache HTTP Server.
The operator m//
(matches) allows you to check a match using a regular expression. (For brevity, the preceding m
can be omitted.) In the simplest case, an expression like:
$x = m/abc/
evaluates to true if and only if the string $x
matches the regular expression abc
.
Parts of the regular expression can be enclosed in parentheses: the corresponding parts of a matched string are captured. The captured strings are sequentially assigned to the internal variables $1, $2, $3,...
and a list of captured strings is returned as the value of the match.
$x = m/a(.)c/; # captures the character between 'a' and 'c' and saves it at $1
The operator s///
(substitution) specifies a search and replace operation:
$x = s/abc/aBc/; # Converts b to capital
Perl regular expressions can take a few modifiers. They are single letter suffixes that modify the meaning of the expression:
$x = m/abc/i; # pairing regardless of whether they're in capital or tiny$x = s/abc/aBc/g; # global search and replacement (through the whole ''string')
Regular expressions can be dense and cryptic. This is because the syntax of regular expressions is extremely compact, generally using single characters or pairs of characters to represent their operations. Perl alleviates this problem somewhat with the /x
modifier that allows programmers to put white space and comments within regular expressions:
$x = m/a match a 'a'# match any character c # match a 'c' /x;
A common use of regular expressions is to specify field delimiters to the split
operator:
@palabras = split m/,/, $line; # divides the securities line separated by commas
The split
operator complements string capture. String capture returns the parts of a string that match a regular expression; split
returns the parts that don't match.
Interface with databases
Perl is widely favored for database applications. Its text handling facilities are good for generating SQL queries; arrays, hashes and automatic memory management make it easy to collect and process the returned data.
In early versions of Perl, database interfaces were created by linking the interpreter with a database library on the client side. This was kind of awkward; One problem in particular was that the resulting perl
executable was restricted to using only one database interface, the one that had been bound. Also, rebinding the interpreter was difficult enough that it was only done for some of the most famous and important databases.
In Perl 5, the database interfaces are implemented by the Perl DBI module. The DBI
module presents a single database-independent interface to Perl applications, while the DBD::
(Database Driver) modules handle the details. access to some 50 different databases. There are DBD::
drivers for most ANSI SQL databases.
Performance comparison
The "Computer Language Shootout Benchmarks" compares the performance of implementations of typical programming problems, in various languages. Their Perl implementations typically take more memory than implementations in other languages, and this varies speed results. Perl's features are similar to those of other languages such as Python, PHP, or Ruby, but slower than most compiled languages.
Perl can be slower than other languages doing the same thing because it has to compile the source code every time the program is run. In & # 34; A Timely Start & # 34;, Jean-Louis Leroy found that his Perl scripts took much longer to run than he expected because the perl interpreter spent most of its time looking up and compiling the scripts. modules. Because Perl cannot save its intermediate compilation like Java, Python, and Ruby can, Perl scripts carry this overhead on every run. Overhead is not a problem when the execution phase is very long, but it can skew significantly at very short execution times, as is often found in benchmarks. Once perl starts the execution phase, however, it can be very fast and typically outperforms other dynamic languages. Technologies like mod perl overcome this by saving the compiled program in memory between runs, or Class::Autouse which delays compiling parts of the program until they are needed.
Optimizing
Nicholas Clark, a core Perl developer, discusses some design improvements with Perl and other solutions in "When perl is not quite fast enough". The most critical routines in a Perl program can be written in C or even in assembly language with XS or Inline.
Optimizing Perl may require an intimate knowledge of its operation rather than skill with the language and its syntax, meaning that the problem is with Perl's implementation rather than the language itself. Raku, the next version, will take some of these ideas as lessons, which other languages have already learned.
Future
In 2000, at the Perl conference, Jon Orwant made a request for a new version of the language. This led to a decision to begin work on redesigning the language, to be called Perl 6. Proposals were solicited from the Perl community for new language features, and more than 300 RFCs (Request For Comments) were received.
Larry Wall spent the next few years digesting the RFCs and synthesizing them into a coherent framework for Perl 6. He presented his Perl 6 design in a series of documents called Revelations, which are numbered to correspond with the chapters. Programming Perl. The current, unfinished Perl 6 specification is summarized in design documents called Synopses, which are numbered to correspond to the Apocalypses.
Perl 6 is not intended to be backwards compatible, although a compatibility mode will exist.
In 2001 it was decided that Perl 6 would run on a virtual machine called Parrot. This means that other languages using Parrot will be able to gain native access to CPAN and will allow for some level of cross-development.
In 2005 Audrey Tang created the pugs project, an implementation of Perl 6 in Haskell. It was and is a testing platform for the Perl 6 language (separate from the actual development of the implementation) allowing designers to explore. The pugs project spawned an active Perl/Haskell community centered around the #perl6 irc channel on Freenode.
A number of features in the Perl 6 language show a similarity to those of Haskell, and Perl 6 has been embraced by the Haskell community as a potential scripting language.
As of 2006, Perl 6, Parrot and pugs are still under development and a new module for Perl 5 called v6 allows a portion of Perl 6 code to run directly on top of Perl 5.
In October 2019, given the evidence that Perl 6 is a very different language from Perl 5, and to avoid confusion between the two languages, it was decided to rename it Raku.
Fun with Perl
As in C, obfuscated code competitions are a popular feature of Perl culture. The Obfuscated Perl contest honors the virtue of Perl's syntactic flexibility. The following program prints the text "Just another Perl / Unix hacker", using 32 parallel processes coordinated with pipes. The full explanation is available on the author's website.
@P=split//,"URRUUc8R";@d=split//,"nrekcah xinU / lreP rehtona tsuJ";sub p{@p{"r$p","u$p"!=(P,P);pipe"r$p","u$p";+$p;($q♪2)+=$f=!fork;map{$=$[chuckles]$f^Ord($p{$_}"6];$p{$_!=/ ^$P/ix?$P:close$_!keys%%!p;p;p;p;p;map{$p{$_!=/^[P.]/" fake "close.$_!%%;Wait. until$?;map{/^r/" fake "≤3!%%;$_=$d[chuckles]$q];sleep rand(2)if/S/;print
Similar to obfuscated code but with a different purpose, "Perl Poetry" it is the practice of writing poems that can be compiled into legal Perl code (although generally nonsense). This hobby is more or less unique to Perl due to the large number of normal English words that the language uses. New poems are posted regularly on the Perl Monks site, in the Perl Poetry section. Some of the Perl lore is in Black Perl, an infamous example of Perl poetics.
Another hobby is "Perl Golf". As with real sport, the goal is to reduce the number of hits needed to complete an objective, but here, the "hits" they refer to keystrokes rather than golf club strokes. A task is proposed, such as "scan an input string and return the largest palindrome it contains" and the participants try to beat their opponents by writing solutions that require fewer and fewer characters of Perl source code.
Another tradition among Perl hackers is to write JAPHs, which are sorts of little obfuscated programs that print the phrase "Just another Perl hacker,". The "canonical" JAPH includes the trailing comma, although it is often omitted, and many other variants that have been created (eg, which prints "Just Another Perl Pirate!").
An interesting Perl module is Lingua::Romana::Perligata (on CPAN). This module translates the source code of a script written in Latin into Perl, allowing the programmer to write executable programs in Latin.
The Perl community has reserved the "Acme" for modules that are fun or experimental. Some of the Acme modules are implemented in very entertaining ways. Some examples:
- Acme::Bleach, one of the first modules in Acme::, allows the source code of a program to be "white" (i.e. all characters are replaced by blank space) and to continue working. It's an example of a source code filter. There are also a certain number of other source filters in the Acme namespace.
- Acme::Hello simplifies the process of writing a program "Hello, World!"
- Acme::Currency allows to change the "$" prefix of the scalar variables by another character (by default use the euro "€")
- Acme::ProgressBar is, completely done on purpose, a horrible and inefficient way to indicate the progress of a task
- Acme::VerySign satirizes the long criticized VeriSign Site Finder service
- Acme::Don't implements the logical opposite of the keyword
do
-don't
— that doesn't run the subordinate code block.
Related links
- Humor Perl on Wikibooks
- Lingua::Romana:Perligata - Write Perl in Latin!
- A tutorial on Perligata
- Perl Purity Test
Perl Community
The Perl community is made up of groups that in turn make up the Perl Mongers.
In Spain there are three groups:
- Barcelona.pm (contact person: Álex Muntada)
- Madrid.pm (contact person: Diego Kuperman)
- Granada.pm (contact person: Juan Julián Merelo Guervós
Contenido relacionado
MediaWiki:Noconnect
Apollon (P2P Client)
DCE