Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Perl
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Design == {{Main|Perl language structure}} === Philosophy === According to Wall, Perl has two slogans. The first is "There's more than one way to do it," commonly known as TMTOWTDI, (pronounced ''Tim Toady''). As proponents of this motto argue, this philosophy makes it easy to write concise statements.<ref>{{cite web |last1=Richardson |first1=Marjorie |title=Larry Wall, the Guru of Perl {{!}} Linux Journal |url=https://www.linuxjournal.com/article/3394 |website=www.linuxjournal.com |publisher=Linux Journal |access-date=16 January 2023}}</ref><ref>{{Cite journal |last=Schwartz |first=Alan |date=December 1998 |title=Tutorial: Perl, a psychologically efficient reformatting language |journal=Behavior Research Methods, Instruments, & Computers |language=en |volume=30 |issue=4 |pages=605–609 |doi=10.3758/BF03209477 |s2cid=61028367 |issn=0743-3808|doi-access=free}}</ref><ref>{{Cite journal |last=Gilbert |first=James G. R. |date=March 2002 |title=How to become a programming tadpole |url=http://www.nature.com/articles/nbt0302-221 |journal=Nature Biotechnology |language=en |volume=20 |issue=3 |pages=221 |doi=10.1038/nbt0302-221 |s2cid=38728402 |issn=1087-0156}}</ref> The second slogan is "Easy things should be easy and hard things should be possible".<ref name="programmingperl2"/> The design of Perl can be understood as a response to three broad trends in the computer industry: falling hardware costs, rising labor costs, and improvements in [[compiler]] technology. Many earlier computer languages, such as [[Fortran]] and C, aimed to make efficient use of expensive computer hardware. In contrast, Perl was designed so that computer programmers could write programs more quickly and easily.<ref>{{Cite web |date=February 13, 2014 |title=The Fall Of Perl, The Web's Most Promising Language |url=https://www.fastcompany.com/3026446/the-fall-of-perl-the-webs-most-promising-language |website=FastCompany |last1=Myhrvold |first1=Conor }}</ref> Perl has many features that ease the task of the programmer at the expense of greater [[CPU]] and memory requirements. These include automatic memory management; [[dynamic typing]]; strings, lists, and hashes; regular expressions; [[type introspection|introspection]]; and an <code>eval()</code> function. Perl follows the theory of "no built-in limits",<ref name="schwartz01"/> an idea similar to the [[Zero One Infinity]] rule. Wall was trained as a linguist, and the design of Perl is very much informed by [[linguistic]] principles. Examples include [[Huffman coding]] (common constructions should be short), good end-weighting (the important information should come first), and a large collection of [[language primitive]]s. Perl favors language constructs that are concise and natural for humans to write, even where they complicate the Perl interpreter.<ref>{{Cite web|last=Wall|first=Larry|title=perl - The Perl 5 language interpreter - Perldoc Browser|url=https://perldoc.perl.org/perl|access-date=2021-06-24|website=perldoc.perl.org}}</ref> Perl's [[Syntax (programming languages)|syntax]] reflects the idea that "things that are different should look different."<ref name="wall97">{{cite journal |url=http://www.linuxjournal.com/article/2070 |title=Wherefore Art, Thou? |access-date=2011-03-13 |last=Wall |first=Larry |date=1997-03-01 |journal=[[Linux Journal]] |archive-date=December 9, 2010 |archive-url=https://web.archive.org/web/20101209021107/http://www.linuxjournal.com/article/2070 |url-status=live}}</ref> For example, scalars, arrays, and hashes have different leading sigils. Array indices and hash keys use different kinds of braces. Strings and regular expressions have different standard delimiters. There is a broad practical bent to both the Perl language and the community and culture that surround it. The preface to ''Programming Perl'' begins: "Perl is a language for getting your job done."<ref name="programmingperl2"/> One consequence of this is that Perl is not a tidy language. It includes many features, tolerates exceptions to its rules, and employs [[heuristics]] to resolve syntactical ambiguities. Because of the forgiving nature of the compiler, bugs can sometimes be hard to find. Perl's function documentation remarks on the variant behavior of built-in functions in list and scalar contexts by saying, "In general, they do what you want, unless you want consistency."<ref name="perlfunc">{{cite web |url=http://perldoc.perl.org/perlfunc.html |title=perlfunc - Perl builtin functions |access-date=2011-01-10 |work=Perl 5 version 12.2 documentation |publisher=perldoc.perl.org |archive-date=January 6, 2011 |archive-url=https://web.archive.org/web/20110106003034/http://perldoc.perl.org/perlfunc.html |url-status=live}}</ref> === Features === The overall structure of Perl derives broadly from C. Perl is [[procedural programming|procedural]] in nature, with [[variable (programming)|variable]]s, [[expression (programming)|expressions]], [[assignment statement]]s, [[bracket|brace]]-delimited [[block (programming)|block]]s, [[control structure]]s, and [[subroutine]]s.<ref>{{Cite book|last=Nagpal|first=D.P.|title=Web Design Technology|publisher=S. Chand|year=2010|isbn=978-8121927635|location=India|pages=700|language=English}}</ref> Perl also takes features from shell programming. All variables are marked with leading [[sigil (computer programming)|sigils]], which allow variables to be [[Variable interpolation|interpolated]] directly into [[String (computer science)|strings]]. However, unlike the shell, Perl uses sigils on all accesses to variables, and unlike most other programming languages that use sigils, the sigil doesn't denote the type of the variable but the type of the expression. So for example, while an array is denoted by the sigil "@" (for example <code>@arrayname</code>), an individual member of the array is denoted by the scalar sigil "$" (for example <code>$arrayname[3]</code>). Perl also has many built-in functions that provide tools often used in shell programming (although many of these tools are implemented by programs external to the shell) such as [[Ascending order|sorting]], and calling [[operating system]] facilities.{{Citation needed|date=December 2020}} Perl takes [[associative array|hashes]] ("associative arrays") from [[AWK]] and [[regular expression]]s from [[sed]]. These simplify many parsing, text-handling, and data-management tasks. Shared with [[Lisp (programming language)|Lisp]] is the implicit [[Return statement|return]] of the last value in a block, and all statements are also expressions which can be used in larger expressions themselves.{{Citation needed|date=December 2020}} Perl 5 added features that support complex [[data structure]]s, [[first-class function]]s (that is, [[Closure (computer science)|closures]] as values), and an object-oriented programming model. These include [[reference (computer science)|references]], packages, class-based [[Dynamic dispatch|method dispatch]], and [[Scope (programming)|lexically scoped variables]], along with [[compiler directive]]s (for example, the <code>strict</code> [[Directive (programming)|pragma]]). A major additional feature introduced with Perl 5 was the ability to package code as reusable modules. Wall later stated that "The whole intent of Perl 5's module system was to encourage the growth of Perl culture rather than the Perl core."<ref>{{cite newsgroup |title=title unknown |date=1997-05-10 |message-id=199705101952.MAA00756@wall.org}}{{Dead link|date=November 2014}}</ref> All versions of Perl do automatic [[Type system|data-typing]] and automatic [[memory management]]. The interpreter knows the type and [[Computer data storage|storage]] requirements of every data object in the program; it allocates and frees storage for them as necessary using [[reference counting]] (so it cannot deallocate [[circular dependency|circular data structures]] without manual intervention). Legal [[type conversion]]s – for example, conversions from number to string – are done automatically at [[Run time (program lifecycle phase)|run time]]; illegal type conversions are fatal errors.{{Citation needed|date=December 2020}} === Syntax === Perl has been referred to as "[[wikt:line noise|line noise]]" and a "write-only language" by its critics. [[Randal L. Schwartz]] in the first edition of the book ''[[Learning Perl]]'',<ref>{{cite journal |title=Developer Update|volume=2 |journal=Dr. Dobb's Developer Update |publisher=Miller-Freeman |year=1995 |page=15}}</ref> in the first chapter states: {{qi|Yes, sometimes Perl looks like line noise to the uninitiated, but to the seasoned Perl programmer, it looks like [[checksum]]med line noise with a mission in life.<ref name="LP">{{cite book |title=Learning Perl |last=Schwartz |first=Randal L. |author-link=Randal L. Schwartz |publisher=O'Reilly & Associates |year=1993 |title-link=Learning Perl |bibcode=1993lepe.book.....S}}</ref>|i=yes}} He also stated that the accusation that Perl is a [[write-only language]] could be avoided by coding with "proper care".<ref name="LP"/> The Perl overview document ''{{Not a typo|perlintro}}'' states that the names of built-in "magic" scalar [[Variable (computer science)|variables]] "look like punctuation or line noise".<ref>{{cite web |url=http://perldoc.perl.org/perlintro.html |title=perlintro |work=Perl 5 version 18.0 documentation |publisher=Perl 5 Porters and perldoc.perl.org |access-date=2013-06-30 |archive-url=https://web.archive.org/web/20110109121845/http://perldoc.perl.org/perlintro.html |archive-date=January 9, 2011 |url-status=dead}}</ref> However, the English module provides both long and short English alternatives. ''{{Not a typo|perlstyle}}'' document states that line noise in [[regular expression]]s could be mitigated using the <code>/x</code> modifier to add whitespace.<ref>{{cite web |url=http://perldoc.perl.org/perlstyle.html |title=perlstyle |work=Perl 5 version 18.0 documentation |publisher=Perl 5 Porters and perldoc.perl.org |access-date=2013-06-30 |archive-url=https://web.archive.org/web/20130626010707/http://perldoc.perl.org/perlstyle.html |archive-date=June 26, 2013 |url-status=dead}}</ref> According to the ''Perl 6 FAQ'',<ref name="P6FAQ">{{cite web |url=http://www.perl6.org/archive/faq.html |title=Perl 6 FAQ |publisher=Perl 6 Project |access-date=2013-06-30 |archive-date=July 1, 2013 |archive-url=https://web.archive.org/web/20130701183900/http://www.perl6.org/archive/faq.html |url-status=dead}}</ref> Perl 6 was designed to mitigate "the usual suspects" that elicit the "line noise" claim from Perl 5 critics, including the removal of "the majority of the punctuation variables" and the sanitization of the regex syntax. The ''Perl 6 FAQ'' also states that what is sometimes referred to as Perl's line noise is "the actual syntax of the language" just as [[gerund]]s and [[Preposition and postposition|prepositions]] are a part of the [[English language]].<ref name="P6FAQ"/> In a December 2012 blog posting, despite claiming that "Rakudo Perl 6 has failed and will continue to fail unless it gets some adult supervision", [[chromatic (programmer)|chromatic]] stated that the design of Perl 6 has a "well-defined grammar", an {{qi|improved type system, a unified object system with an intelligent [[metamodel]], metaoperators, and a clearer system of context that provides for such niceties as pervasive laziness.<ref name="chromatic-blog">{{cite web |url=http://www.modernperlbooks.com/mt/2012/12/the-implementation-of-perl-5-versus-perl-6.html |title=The Implementation of Perl 5 versus Perl 6 |author1=chromatic |author1-link=chromatic (programmer) |date=2012-12-31 |access-date=2013-06-30 |archive-date=July 29, 2013 |archive-url=https://web.archive.org/web/20130729214209/http://www.modernperlbooks.com/mt/2012/12/the-implementation-of-perl-5-versus-perl-6.html |url-status=live}}</ref>|i=yes}} He also stated that {{qi|Perl 6 has a coherence and a consistency that Perl 5 lacks.<ref name="chromatic-blog"/>|i=yes}} In Perl, one could write the [["Hello, World!" program]] as: <syntaxhighlight lang="perl"> print "Hello, World!\n"; </syntaxhighlight> Here is a more complex Perl program, that counts down seconds from a given starting value: <syntaxhighlight lang="perl"> #!/usr/bin/env perl use strict; use warnings; my ( $remaining, $total ); $remaining=$total=shift(@ARGV); STDOUT->autoflush(1); while ( $remaining ) { printf ( "Remaining %s/%s \r", $remaining--, $total ); sleep 1; } print "\n"; </syntaxhighlight> The Perl interpreter can also be used for [[One-liner program|one-off scripts]] on the [[command line]]. The following example (as invoked from an sh-compatible shell, such as [[Bash (Unix shell)|Bash]]) translates the string "Bob" in all files ending with .txt in the current directory to "Robert": <syntaxhighlight lang="console"> $ perl -i.bak -lp -e 's/Bob/Robert/g' *.txt </syntaxhighlight> === Implementation === No written [[Formal specification|specification]] or standard for the Perl language exists for Perl versions through Perl 5, and there are no plans to create one for the current version of Perl. There has been only one implementation of the interpreter, and the language has evolved along with it. That interpreter, together with its functional tests, stands as a ''de facto'' specification of the language. Perl 6, however, started with a specification,<ref>{{cite web |url=http://www.perl6.org/specification |title=Perl 6 Specification |access-date=2011-01-27 |publisher=The Perl 6 Project |archive-date=December 2, 2009 |archive-url=https://web.archive.org/web/20091202073507/http://www.perl6.org/specification/ |url-status=live}}</ref> and several projects<ref>{{cite web |url=http://www.perl6.org/compilers/ |title=Perl 6 Compilers |access-date=2011-01-27 |publisher=The Perl 6 Project |archive-date=December 2, 2009 |archive-url=https://web.archive.org/web/20091202073302/http://www.perl6.org/compilers/ |url-status=live}}</ref> aim to implement some or all of the specification.{{Citation needed|date=December 2020}} Perl is implemented as a core interpreter, written in C, together with a large collection of modules, written in Perl and C. {{As of|2010}}, the interpreter is 150,000 lines of C code and compiles to a 1 MB executable on typical machine architectures. Alternatively, the interpreter can be compiled to a link library and embedded in other programs. There are nearly 500 modules in the distribution, comprising 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).{{Citation needed|date=December 2020}} The interpreter has an object-oriented architecture. All of the elements of the Perl language—scalars, arrays, hashes, coderefs, [[file handle]]s—are represented in the interpreter by [[struct (C programming language)|C structs]]. Operations on these structs are defined by a large collection of [[Macro (computer science)|macros]], [[typedef]]s, and functions; these constitute the Perl C [[application programming interface|API]]. The Perl API can be bewildering to the uninitiated, but its entry points follow a consistent [[naming scheme]], which provides guidance to those who use it.{{Citation needed|date=December 2020}} The life of a Perl interpreter divides broadly into a compile phase and a run phase.<ref>A description of the Perl 5 interpreter can be found in ''Programming Perl'', 3rd Ed., chapter 18. See particularly page 467, which carefully distinguishes run phase and compile phase from [[Run time (program lifecycle phase)|run time]] and [[compile time]]. Perl "time" and "phase" are often confused.</ref> According to Aluín et al., "Perl cannot be parsed by a straight Lex/Yacc lexer/parser combination. Instead, the interpreter implements its own lexer, which coordinates with a modified GNU bison parser to resolve ambiguities in the language."<ref>{{Cite book |last1=Abuin |first1=Jose M. |last2=Pichel |first2=Juan C. |last3=Pena |first3=Tomas F. |last4=Gamallo |first4=Pablo |last5=Garcia |first5=Marcos |chapter=Perldoop: Efficient execution of Perl scripts on Hadoop clusters |date=2014 |title=2014 IEEE International Conference on Big Data (Big Data) |chapter-url=https://ieeexplore.ieee.org/document/7004303 |publisher=IEEE |pages=766–771 |doi=10.1109/BigData.2014.7004303 |isbn=978-1-4799-5666-1}}</ref> Most of what happens in Perl's compile phase is compilation, and most of what happens in Perl's run phase is execution, but there are significant exceptions. Perl makes important use of its capability to execute Perl code during the compile phase. Perl will also delay compilation into the run phase. The terms that indicate the kind of processing that is actually occurring at any moment are ''compile time'' and ''run time''. Perl is in compile time at most points during the compile phase, but compile time may also be entered during the run phase. The compile time for code in a string argument passed to the <code>[[eval]]</code> built-in occurs during the run phase. Perl is often in run time during the compile phase and spends most of the run phase in run time. Code in <code>BEGIN</code> blocks executes at run time but in the compile phase.<!-- NOTE TO EDITORS: There is something missing in the preceding sentence -- for one thing, a comma before the conjunction. The sentence might reasonably read 'Code in BEGIN blocks executes NOT at run time, but in the compile phase,' but is that what the author intended? --> At compile time, the interpreter parses Perl code into a [[Abstract syntax tree|syntax tree]]. At run time, it executes the program by [[Tree traversal|walking the tree]]. Text is parsed only once, and the syntax tree is subject to optimization before it is executed, so that execution is relatively efficient. Compile-time optimizations on the syntax tree include [[constant folding]] and context propagation, but [[peephole optimization]] is also performed.<ref>{{Cite web|title=perlguts - Introduction to the Perl API - Perldoc Browser|url=https://perldoc.perl.org/perlguts#Compile-pass-3:-peephole-optimization|access-date=2022-01-24|website=perldoc.perl.org}}</ref> Perl has a [[Turing-complete]] [[formal grammar|grammar]] because parsing can be affected by run-time code executed during the compile phase.<ref>{{cite web |last=Schwartz |first=Randal |author-link=Randal L. Schwartz |title=On Parsing Perl |url=http://www.perlmonks.org/index.pl?node_id=44722 |access-date=2007-01-03 |archive-date=September 27, 2007 |archive-url=https://web.archive.org/web/20070927000827/http://www.perlmonks.org/index.pl?node_id=44722 |url-status=live}}</ref> The code cannot be parsed by a straight [[Lex programming tool|Lex]]/[[Yacc]] [[Lexical analysis|lexer]]/[[parser]]. To resolve ambiguities in the language the interpreter must implement its own lexer to coordinate with a modified [[GNU bison]] parser.<ref>{{Cite book |last1=Abuin |first1=Jose M. |last2=Pichel |first2=Juan C. |last3=Pena |first3=Tomas F. |last4=Gamallo |first4=Pablo |last5=Garcia |first5=Marcos |chapter=Perldoop: Efficient execution of Perl scripts on Hadoop clusters |date=2014 |title=2014 IEEE International Conference on Big Data (Big Data) |chapter-url=https://ieeexplore.ieee.org/document/7004303 |publisher=IEEE |pages=766–771 |doi=10.1109/BigData.2014.7004303 |isbn=978-1-4799-5666-1}}</ref> It is often said that "Only perl can parse Perl",<ref>{{cite web |url=ftp://ftp.ora.com/pub/labs/tpj/tpj2.pdf |title=The Perl Journal #19/9.26 |access-date=2011-02-04 |archive-url=https://web.archive.org/web/20200227002839/ftp://ftp.ora.com/pub/labs/tpj/tpj2.pdf |archive-date=2020-02-27 |url-status=dead |publisher=[[O'Reilly Media]] }}</ref> meaning that only the Perl interpreter (''<code>perl</code>'') can parse the Perl language (''Perl''), but even this is not, in general, true. Because the Perl interpreter can simulate a Turing machine during its compile phase, it would need to decide the [[halting problem]] in order to complete parsing in every case. It is a longstanding result that the halting problem is undecidable, and therefore not even Perl can always parse Perl. Perl makes the unusual choice of giving the user access to its full programming power in its own compile phase. The cost in terms of theoretical purity is high, but practical inconvenience seems to be rare.<ref>{{cite web |url=http://blogs.perl.org/users/jeffrey_kegler/2011/10/perl-and-parsing-11-are-all-perl-programs-parseable.html |title=Perl and Parsing 11: Are all Perl programs parseable? |last=Kegler |first=Jeffrey |date=October 7, 2011 |access-date=October 25, 2022 |quote=... we seem to be giving up absolutely nothing. Nobody has ever been able to show a practical downside}}</ref> Other programs that undertake to parse Perl, such as [[Static program analysis|source-code analyzers]] and [[Indent style|auto-indenters]], have to contend not only with ambiguous [[Language construct|syntactic constructs]] but also with the [[Recursive language|undecidability]] of Perl parsing in the general case. [[Adam Kennedy (programmer)|Adam Kennedy]]'s PPI project focused on parsing Perl code as a document (retaining its integrity as a document), instead of parsing Perl as executable code (that not even Perl itself can always do). It was Kennedy who first conjectured that "parsing Perl suffers from the 'halting problem',"<ref>{{cite web |url=https://metacpan.org/module/PPI |title=PPI—Parse, Analyze and Manipulate Perl (without perl) |last1=Kennedy |first1=Adam |year=2006 |publisher=[[CPAN]] |access-date=September 16, 2013 |archive-date=September 3, 2013 |archive-url=https://web.archive.org/web/20130903091241/https://metacpan.org/module/PPI |url-status=live}}</ref> which was later proved.<ref>{{cite journal |title=Rice's Theorem |journal=The Perl Review |volume=4 |issue=3 |pages=23–29 |date=Summer 2008}} and {{cite journal |title=Perl is Undecidable |journal=The Perl Review |volume=5 |pages=7–11 |date=Fall 2008}}, available online at {{cite web |url=http://www.jeffreykegler.com/Home/perl-and-undecidability |title=Perl and Undecidability |last1=Kegler|first1=Jeffrey |access-date=January 4, 2009 |archive-date=August 17, 2009 |archive-url=https://web.archive.org/web/20090817183115/http://www.jeffreykegler.com/Home/perl-and-undecidability |url-status=live}}</ref> Perl is distributed with over 250,000 [[Functional testing|functional tests]] for core Perl language and over 250,000 functional tests for core modules. These run as part of the normal build process and extensively exercise the interpreter and its core modules. Perl developers rely on the functional tests to ensure that changes to the interpreter do not introduce [[software bug]]s; further, Perl users who see that the interpreter passes its functional tests on their system can have a high degree of confidence that it is working properly.{{Citation needed|date=December 2020}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)