Perl language structure
The structure of the Perl programming language encompasses both the syntactical rules of the language and the general ways in which programs are organized. Perl's design philosophy is expressed in the commonly cited motto "there's more than one way to do it". As a multi-paradigm, dynamically typed language, Perl allows a great degree of flexibility in program design. Perl also encourages modularization; this has been attributed to the component-based design structure of its Unix roots[when?],[1] and is responsible for the size of the CPAN archive, a community-maintained repository of more than 100,000 modules.[2] Basic syntaxIn Perl, the minimal Hello World program may be written as follows: print "Hello, World!\n"
This prints the string Hello, World! and a newline, symbolically expressed by an say "Hello, World!"
An entire Perl program may also be specified as a command-line parameter to Perl, so the same program can also be executed from the command line (example shown for Unix): $ perl -e 'print "Hello, World!\n"'
The canonical form of the program is slightly more verbose: #!/usr/bin/env perl
print "Hello, World!\n";
The hash mark character introduces a 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’s called the shebang line. This tells Unix-like operating systems to find the Perl interpreter, making it possible to invoke the program without explicitly mentioning 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. Version 5.10 of Perl introduces a use 5.010; # must be present to import the new 5.10 functions, notice that it is 5.010 not 5.10
say 'Hello, World!'
Data typesPerl has a number of fundamental data types. The most commonly used and discussed are scalars, arrays, hashes, filehandles, and subroutines:
Scalar valuesString values (literals) must be enclosed by quotes. Enclosing a string in double quotes allows the values of variables whose names appear in the string to automatically replace the variable name (or be interpolated) in the string. Enclosing a string in single quotes prevents variable interpolation. For example, if
To include a double quotation mark in a string, precede it with a backslash or enclose the string in single quotes. To include a single quotation mark, precede it with a backslash or enclose the string in double quotes. Strings can also be quoted with the
Finally, multiline strings can be defined using here documents: $multilined_string = <<EOF;
This is my multilined string
note that I am terminating it with the word "EOF".
EOF
Numbers (numeric constants) do not require quotation. Perl will convert numbers into strings and vice versa depending on the context in which they are used. When strings are converted into numbers, trailing non-numeric parts of the strings are discarded. If no leading part of a string is numeric, the string will be converted to the number 0. In the following example, the strings $n = '3 apples';
$m = '2 oranges';
print $n + $m;
Functions are provided for the rounding of fractional values to integer values: Perl also has a boolean context that it uses in evaluating conditional statements. The following values all evaluate as false in Perl: $false = 0; # the number zero
$false = 0.0; # the number zero as a float
$false = 0b0; # the number zero in binary
$false = 0x0; # the number zero in hexadecimal
$false = '0'; # the string zero
$false = ""; # the empty string
$false = (); # the empty list
$false = undef; # the return value from undef
$false = 2-3+1 # computes to 0 that is converted to "0" so it is false
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 '0E0' or '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 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 If either 1 or 0 are specifically needed, an explicit conversion can be done using the conditional operator: my $real_result = $boolean_result ? 1 : 0;
Array valuesAn array value (or list) is specified by listing its elements, separated by commas, enclosed by parentheses (at least where required by operator precedence). @scores = (32, 45, 16, 5);
The qw() quote-like operator allows the definition of a list of strings without typing of quotes and commas. Almost any delimiter can be used instead of parentheses. The following lines are equivalent: @names = ('Billy', 'Joe', 'Jim-Bob');
@names = qw(Billy Joe Jim-Bob);
The split function returns a list of strings, which are split from a string expression using a delimiter string or regular expression. @scores = split(',', '32,45,16,5');
Individual elements of a list are accessed by providing a numerical index in square brackets. The scalar 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, Hash valuesPerl programmers may initialize a hash (or associative array) from a list of key/value pairs. If the keys are separated from the values with the %favorite = ('joe', "red", 'sam', "blue");
%favorite = (joe => 'red', sam => 'blue');
Individual values in a hash are accessed by providing the corresponding key, in curly braces. The $favorite{joe} = 'red';
$favorite{sam} = 'blue';
$favorite{oscar} = 'green';
Multiple elements may be accessed using the FilehandlesFilehandles provide read and write access to resources. These are most often files on disk, but can also be a device, a pipe, or even a scalar value. Originally, filehandles could only be created with package variables, using the ALL_CAPS convention to distinguish it from other variables. Perl 5.6 and newer also accept a scalar variable, which will be set (autovivified) to a reference to an anonymous filehandle, in place of a named filehandle. Typeglob valuesA typeglob value is a symbol table entry. The main use of typeglobs is creating symbol table aliases. For example: *PI = \3.141592653; # creating constant scalar $PI
*this = *that; # creating aliases for all data types 'this' to all data types 'that'
Array functionsThe number of elements in an array can be determined either by evaluating the array in scalar context or with the help of the Hash functionsThere are a few functions that operate on entire hashes. The keys function takes a hash and returns the list of its keys. Similarly, the values function returns a hash's values. Note that the keys and values are returned in a consistent but arbitrary order. # Every call to each returns the next key/value pair.
# All values will be eventually returned, but their order
# cannot be predicted.
while (($name, $address) = each %addressbook) {
print "$name lives at $address\n";
}
# Similar to the above, but sorted alphabetically
foreach my $next_name (sort keys %addressbook) {
print "$next_name lives at $addressbook{$next_name}\n";
}
Control structuresPerl has several kinds of control structures. It has block-oriented control structures, similar to those in the C, JavaScript, and 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: statement if cond ; statement unless cond ; statement while cond ; statement until cond ; statement foreach list ; Short-circuit logical operators are commonly used to affect control flow at the expression level: expr and expr expr && expr expr or expr expr || expr (The "and" and "or" operators are similar to && and || but have lower precedence, which makes it easier to use them to control entire statements.) The flow control keywords 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
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 use v5.10; # must be present to import the new 5.10 functions given ( expr ) { when ( cond ) { … } default { … } } Syntactically, this structure behaves similarly to switch statements 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 For those not using Perl 5.10, the Perl documentation describes a half-dozen ways to achieve the same effect by using other control structures. There is also a Switch module, which provides functionality modeled on that of sister language Raku. It is implemented using a source filter, so its use is unofficially discouraged.[6] Perl includes a There is also a SubroutinesSubroutines are defined with the # Calling a subroutine
# Parentheses are required here if the subroutine is defined later in the code
foo();
&foo; # (this also works, but has other consequences regarding arguments passed to the subroutine)
# Defining a subroutine
sub foo { … }
foo; # Here parentheses are not required
A list of arguments may be provided after the subroutine name. Arguments may be scalars, lists, or hashes. foo $x, @y, %z;
The parameters to a subroutine do not need to be declared as to either number or type; in fact, they may vary from call to call. Any validation of parameters must be performed explicitly inside the subroutine. Arrays are expanded to their elements; hashes are expanded to a list of key/value pairs; and the whole lot is passed into the subroutine as one flat list of scalars. Whatever arguments are passed are available to the subroutine in the special array Elements of $_[0], $_[1]
However, the resulting code can be difficult to read, and the parameters have pass-by-reference semantics, which may be undesirable. One common idiom is to assign my ($x, $y, $z) = @_;
This provides mnemonic parameter names and implements pass-by-value semantics. The Another idiom is to shift parameters off of my $x = shift;
Subroutines may assign sub function1 {
my %args = @_;
print "'x' argument was '$args{x}'\n";
}
function1( x => 23 );
Subroutines may return values. return 42, $x, @y, %z;
If the subroutine does not exit via a The returned expression is evaluated in the calling context of the subroutine; this can surprise the unwary. sub list { (4, 5, 6) }
sub array { @x = (4, 5, 6); @x }
$x = list; # returns 6 - last element of list
$x = array; # returns 3 - number of elements in list
@x = list; # returns (4, 5, 6)
@x = array; # returns (4, 5, 6)
A subroutine can discover its calling context with the sub either {
return wantarray ? (1, 2) : 'Oranges';
}
$x = either; # returns "Oranges"
@x = either; # returns (1, 2)
Anonymous functionsPerl 5 supports anonymous functions,[8] as follows: (sub { print "I got called\n" })->(); # 1. fully anonymous, called as created
my $squarer = sub { my $x = shift; $x * $x }; # 2. assigned to a variable
sub curry {
my ($sub, @args) = @_;
return sub { $sub->(@args, @_) }; # 3. as a return value of another function
}
# example of currying in Perl programming
sub sum { my $tot = 0; $tot += $_ for @_; $tot } # returns the sum of its arguments
my $curried = curry \&sum, 5, 7, 9;
print $curried->(1,2,3), "\n"; # prints 27 ( = 5 + 7 + 9 + 1 + 2 + 3 )
Other constructs take bare blocks as arguments, which serve a function similar to lambda functions of one parameter, but do not have the same parameter-passing convention as functions -- @_ is not set. my @squares = map { $_ * $_ } 1..10; # map and grep don't use the 'sub' keyword
my @square2 = map $_ * $_, 1..10; # braces unneeded for one expression
my @bad_example = map { print for @_ } 1..10; # values not passed like normal Perl function
Regular expressionsThe Perl language includes a specialized syntax for writing regular expressions (RE, or regexes), and the interpreter contains an engine for matching strings to regular expressions. The regular-expression engine uses a backtracking algorithm, extending its capabilities from simple pattern matching to string capture and substitution. The regular-expression engine is derived from regex written by Henry Spencer. The Perl regular-expression syntax was originally taken from Unix Version 8 regular expressions. However, it diverged before the first release of Perl and has since grown to include far more features. Many other languages and applications are now adopting Perl Compatible Regular Expressions over POSIX regular expressions, such as PHP, Ruby, Java, Microsoft's .NET Framework,[9] and the Apache HTTP server. Regular-expression syntax is extremely compact, owing to history. The first regular-expression dialects were only slightly more expressive than globs, and the syntax was designed so that an expression would resemble the text that it matches.[citation needed] This meant using no more than a single punctuation character or a pair of delimiting characters to express the few supported assertions. Over time, the expressiveness of regular expressions grew tremendously, but the syntax design was never revised and continues to rely on punctuation. As a result, regular expressions can be cryptic and extremely dense. UsesThe $x =~ /abc/;
evaluates to true if and only if the string The $x =~ s/abc/aBc/; # upcase the b
Another use of regular expressions is to specify delimiters for the @words = split /,/, $line;
The SyntaxModifiersPerl regular expressions can take modifiers. These are single-letter suffixes that modify the meaning of the expression: $x =~ /abc/i; # case-insensitive pattern match
$x =~ s/abc/aBc/g; # global search and replace
Because the compact syntax of regular expressions can make them dense and cryptic, the $x =~ /
a # match 'a'
. # followed by any character
c # then followed by the 'c' character
/x;
CapturingPortions of a regular expression may be enclosed in parentheses; corresponding portions of a matching string are captured. Captured strings are assigned to the sequential built-in variables $x =~ /a(.)c/; # capture the character between 'a' and 'c'
Captured strings Perl regular expressions also allow built-in or user-defined functions to apply to the captured match, by using the $x = "Oranges";
$x =~ s/(ge)/uc($1)/e; # OranGEs
$x .= $1; # append $x with the contents of the match in the previous statement: OranGEsge
Objects
There are many ways to write object-oriented code in Perl. The most basic is using "blessed" references. This works by identifying a reference of any type as belonging to a given package, and the package provides the methods for the blessed reference. For example, a two-dimensional point could be defined this way: sub Point::new {
# Here, Point->new(4, 5) will result in $class being 'Point'.
# It's a variable to support subclassing (see the perloop manpage).
my ($class, $x, $y) = @_;
bless [$x, $y], $class; # Implicit return
}
sub Point::distance {
my ($self, $from) = @_;
my ($dx, $dy) = ($$self[0] - $$from[0], $$self[1] - $$from[1]);
sqrt($dx * $dx + $dy * $dy);
}
This class can be used by invoking my $p1 = Point->new(3, 4);
my $p2 = Point->new(0, 0);
print $p1->distance($p2); # Prints 5
Many modern Perl applications use the Moose object system.[citation needed] Moose is built on top of Class::MOP, a meta-object protocol, providing complete introspection for all Moose-using classes. Thus you can ask classes about their attributes, parents, children, methods, etc. using a simple API. Moose classes:
Moose roles:
ExamplesAn example of a class written using the MooseX::Declare[10] extension to Moose: use MooseX::Declare;
class Point3D extends Point {
has 'z' => (isa => 'Num', is => 'rw');
after clear {
$self->z(0);
}
method set_to (Num $x, Num $y, Num $z) {
$self->x($x);
$self->y($y);
$self->z($z);
}
}
This is a class named References
External links
|
Portal di Ensiklopedia Dunia