Perl language structure: Difference between revisions

Content deleted Content added
m Control structures: <u>comment</u>
 
(4 intermediate revisions by 3 users not shown)
Line 20:
 
<syntaxhighlight lang="perl">
#!/usr/bin/env perl
print "Hello, World!\n";
</syntaxhighlight>
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 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 376:
</syntaxhighlight>
===Anonymous functions===
{{Excerpt|AnonymousExamples functionof anonymous functions|Perl 5|subsections=yes}}
 
==Regular expressions==