Content deleted Content added
m →Data types: {{code}} |
clean up, typo(s) fixed: … → ... (17), it’s → it's |
||
Line 24:
</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:
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'' ) {
''label'' for ( ''init-expr'' ; ''cond-expr'' ; ''incr-expr'' ) {
''label'' foreach ''var'' ( ''list'' ) {
''label'' foreach ''var'' ( ''list'' ) {
if ( ''cond'' ) {
if ( ''cond'' ) {
if ( ''cond'' ) {
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 {
''results'' = grep ''expr'', ''list''
''results'' = map {
''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'' ) {
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|Anonymous function|Perl 5|subsections=yes}}
|