Perl language structure: Difference between revisions

Content deleted Content added
m change source to syntaxhighlight
 
(19 intermediate revisions by 12 users not shown)
Line 3:
 
== Basic syntax ==
In Perl, the minimal [["Hello, World!" program|Hello World]] program may be written as follows:
<syntaxhighlight lang="perl">
print "Hello, World!\n"
Line 20:
 
<syntaxhighlight lang="perl">
#!/usr/bin/env perl
print "Hello, World!\n";
</syntaxhighlight>
Line 40:
 
{| class="wikitable"
|-bol
! Type
! Sigil
Line 47:
|-
|[[Scalar (computing)|Scalar]]
|{{tt|$}}
|{{code|$foo}}
|A single value; it may be a number, a [[String (computer science)|string]], a filehandle, or a [[Reference (computer science)|reference]].
|-
|[[Array data type|Array]]
|{{tt|@}}
|{{code|@foo}}
|An ordered collection of scalars.
|-
|[[Associative array|Hash]]
|{{tt|%}}
|{{code|%foo}}
|A map from strings to scalars; the strings are called ''keys'', and the scalars are called ''values''. Also known as an ''associative array''.
|-
|[[FilehandleFile handle]]
|{{CNone|none}}
|{{code|$foo}} or {{code|FOO}}
|An opaque representation of an open file or other target for reading, writing, or both.
|-
|[[Subroutine]]
|{{tt|&}}
|{{code|&foo}}
|A piece of code that may be passed arguments, be executed, and return data.
|-
|[[Perl language structure#Typeglob values|Typeglob]]
|{{tt|*}}
|{{code|*foo}}
|The [[symbol table]] entry for all types with the name 'foo'.
|}
 
Line 124:
</syntaxhighlight>
 
All other (non-zero evaluating) values evaluate to true. This includes the odd self-describing literal string of "0 but true", which in fact is 0 as a number, but true when used as a boolean. All non-numeric strings also have this property, but this particular string is truncated by Perl without a numeric warning. A less explicit but more conceptually portable version of this string is '{{mono|0E0}}' or '{{mono|0e0}}', which does not rely on characters being evaluated as 0, because '0E0' is literally zero times ten to the power zero. The empty hash <code>{}</code> is also true; in this context <code>{}</code> is not an empty block, because <code>perl -e 'print ref {}'</code> returns <code>HASH</code>.
 
Evaluated boolean expressions are also scalar values. The documentation does not promise which ''particular'' value of true or false is returned. Many boolean operators return 1 for true and the empty-string for false. The ''{{code|defined()''}} function determines whether a variable has any value set. In the above examples, ''{{code|defined($false)''}} is true for every value except ''{{code|undef''}}.
 
If either 1 or 0 are specifically needed, an explicit conversion can be done using the [[conditional operator]]:
Line 155:
</syntaxhighlight>
 
Individual elements of a list are accessed by providing a numerical index in square brackets. The scalar [[sigil (computer programming)|sigil]] must be used. Sublists (array slices) can also be specified, using a range or list of numeric indices in brackets. The array sigil is used in this case. For example, <code>$month[3]</code> is <code>"April"</code> (the first element in an array has an index value of 0), and <code>@month[4..6]</code> is <code>("May", "June", "July")</code>.
 
===Hash values===
Line 180:
</syntaxhighlight>
 
Individual values in a hash are accessed by providing the corresponding key, in curly braces. The <code>$</code> sigil identifies the accessed element as a scalar. For example, {{code|$favorite{joe} }} equals {{code|'red'}}. A hash can also be initialized by setting its values individually:
 
<syntaxhighlight lang="perl">
Line 189:
 
Multiple elements may be accessed using the <code>@</code> sigil instead (identifying the result as a list). For example,
{{code|@favorite{'joe', 'sam'} }} equals {{code|('red', 'blue')}}.
 
===Filehandles===
Line 208:
===Array functions===
 
The number of elements in an array can be determined either by evaluating the array in scalar context or with the help of the <code>$#</code> sigil. The latter gives the index of the last element in the array, not the number of elements. The expressions scalar({{code|@array}}) and (<code>$#array&nbsp;+&nbsp;1</code>) are equivalent.
 
===Hash functions===
Line 274:
Up until the 5.10.0 release, there was no [[switch statement]] in Perl 5. From 5.10.0 onward, a multi-way branch statement called <code>given</code>/<code>when</code> is available, which takes the following form:
 
use v5.10; <u># must be present to import the new 5.10 functions</u>
given ( ''expr'' ) { when ( ''cond'' ) { … } default { … } }
 
Line 375:
@x = either; # returns (1, 2)
</syntaxhighlight>
===Anonymous functions===
{{Excerpt|Examples of anonymous functions|Perl 5|subsections=yes}}
 
==Regular expressions==