Perl language structure: Difference between revisions

Content deleted Content added
clean up, typo(s) fixed: … → ... (17), it’s → it's
Tags: AWB Reverted
 
(2 intermediate revisions by 2 users not shown)
Line 20:
 
<syntaxhighlight lang="perl">
#!/usr/bin/env perl
print "Hello, World!\n";
</syntaxhighlight>
 
The hash mark character introduces a [[comment (computer programming)|comment]] in Perl, which runs up to the end of the line of code and is ignored by the compiler (except on Windows). The comment used here is of a special kind: it'sit’s called the [[Shebang (Unix)|shebang]] line. This tells Unix-like operating systems to find the Perl interpreter, making it possible to invoke the program without explicitly mentioning <code>perl</code>. (Note that, on [[Microsoft Windows]] systems, Perl programs are typically invoked by associating the <code>.pl</code> [[Filename extension|extension]] with the Perl interpreter. In order to deal with such circumstances, <code>perl</code> detects the shebang line and parses it for switches.<ref>{{cite web | url = http://perldoc.perl.org/perlrun.html | title = perlrun | accessdate = 2011-01-08 | publisher = perldoc.perl.org - Official documentation for the Perl programming language}}</ref>)
 
The second line in the canonical form includes a semicolon, which is used to separate statements in Perl. With only a single statement in a block or file, a separator is unnecessary, so it can be omitted from the minimal form of the program—or more generally from the final statement in any block or file. The canonical form includes it, because it is common to terminate every statement even when it is unnecessary to do so, as this makes editing easier: code can be added to, or moved away from, the end of a block or file without having to adjust semicolons.
Line 235:
It has block-oriented control structures, similar to those in the C, [[JavaScript]], and [[Java (programming language)|Java]] programming languages. Conditions are surrounded by parentheses, and controlled blocks are surrounded by braces:
 
''label'' while ( ''cond'' ) { ... }
''label'' while ( ''cond'' ) { ... } continue { ... }
''label'' for ( ''init-expr'' ; ''cond-expr'' ; ''incr-expr'' ) { ... }
''label'' foreach ''var'' ( ''list'' ) { ... }
''label'' foreach ''var'' ( ''list'' ) { ... } continue { ... }
if ( ''cond'' ) { ... }
if ( ''cond'' ) { ... } else { ... }
if ( ''cond'' ) { ... } elsif ( ''cond'' ) { ... } else { ... }
 
Where only a single statement is being controlled, statement modifiers provide a more-concise syntax:
Line 265:
Perl also has two implicit looping constructs, each of which has two forms:
 
''results'' = grep { ... } ''list''
''results'' = grep ''expr'', ''list''
''results'' = map { ... } ''list''
''results'' = map ''expr'', ''list''
 
Line 275:
 
use v5.10; <u># must be present to import the new 5.10 functions</u>
given ( ''expr'' ) { when ( ''cond'' ) { ... } default { ... } }
 
Syntactically, this structure behaves similarly to [[switch statement]]s found in other languages, but with a few important differences. The largest is that unlike switch/case structures, given/when statements break execution after the first successful branch, rather than waiting for explicitly defined break commands. Conversely, explicit <code>continue</code>s are instead necessary to emulate switch behavior.
Line 375:
@x = either; # returns (1, 2)
</syntaxhighlight>
 
===Anonymous functions===
{{Excerpt|AnonymousExamples functionof anonymous functions|Perl 5|subsections=yes}}
 
==Regular expressions==