2013-08-13 18:37:52 +04:00
|
|
|
---
|
|
|
|
name: perl
|
|
|
|
category: language
|
|
|
|
language: perl
|
|
|
|
filename: learnperl.pl
|
|
|
|
contributors:
|
|
|
|
- ["Korjavin Ivan", "http://github.com/korjavin"]
|
2015-12-14 02:03:47 +03:00
|
|
|
- ["Dan Book", "http://github.com/Grinnz"]
|
2013-08-13 18:37:52 +04:00
|
|
|
---
|
|
|
|
|
|
|
|
Perl 5 is a highly capable, feature-rich programming language with over 25 years of development.
|
|
|
|
|
|
|
|
Perl 5 runs on over 100 platforms from portables to mainframes and is suitable for both rapid prototyping and large scale development projects.
|
|
|
|
|
|
|
|
```perl
|
2015-06-20 00:11:38 +03:00
|
|
|
# Single line comments start with a number sign.
|
2013-08-13 18:37:52 +04:00
|
|
|
|
2015-12-14 03:22:29 +03:00
|
|
|
#### Strict and warnings
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
# All perl scripts and modules should include these lines. Strict causes
|
2016-04-15 10:23:10 +03:00
|
|
|
# compilation to fail in cases like misspelled variable names, and
|
|
|
|
# warnings will print warning messages in case of common pitfalls like
|
|
|
|
# concatenating to an undefined value.
|
2015-12-14 03:22:29 +03:00
|
|
|
|
2013-08-13 18:37:52 +04:00
|
|
|
#### Perl variable types
|
|
|
|
|
2015-06-20 00:11:38 +03:00
|
|
|
# Variables begin with a sigil, which is a symbol showing the type.
|
2013-08-13 18:37:52 +04:00
|
|
|
# A valid variable name starts with a letter or underscore,
|
|
|
|
# followed by any number of letters, numbers, or underscores.
|
|
|
|
|
2015-06-20 00:11:38 +03:00
|
|
|
### Perl has three main variable types: $scalar, @array, and %hash.
|
2013-08-13 18:37:52 +04:00
|
|
|
|
|
|
|
## Scalars
|
|
|
|
# A scalar represents a single value:
|
|
|
|
my $animal = "camel";
|
|
|
|
my $answer = 42;
|
|
|
|
|
2014-06-10 21:06:08 +04:00
|
|
|
# Scalar values can be strings, integers or floating point numbers, and
|
|
|
|
# Perl will automatically convert between them as required.
|
2013-08-13 18:37:52 +04:00
|
|
|
|
|
|
|
## Arrays
|
|
|
|
# An array represents a list of values:
|
|
|
|
my @animals = ("camel", "llama", "owl");
|
|
|
|
my @numbers = (23, 42, 69);
|
|
|
|
my @mixed = ("camel", 42, 1.23);
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# Array elements are accessed using square brackets, with a $ to
|
|
|
|
# indicate one value will be returned.
|
2015-12-14 02:03:47 +03:00
|
|
|
my $second = $animals[1];
|
2013-08-13 18:37:52 +04:00
|
|
|
|
|
|
|
## Hashes
|
|
|
|
# A hash represents a set of key/value pairs:
|
|
|
|
|
|
|
|
my %fruit_color = ("apple", "red", "banana", "yellow");
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# You can use whitespace and the "=>" operator to lay them out more
|
|
|
|
# nicely:
|
2013-08-13 18:37:52 +04:00
|
|
|
|
|
|
|
my %fruit_color = (
|
2015-06-10 01:00:12 +03:00
|
|
|
apple => "red",
|
|
|
|
banana => "yellow",
|
|
|
|
);
|
2015-12-14 02:03:47 +03:00
|
|
|
|
|
|
|
# Hash elements are accessed using curly braces, again with the $ sigil.
|
|
|
|
my $color = $fruit_color{apple};
|
|
|
|
|
2014-06-10 21:06:08 +04:00
|
|
|
# Scalars, arrays and hashes are documented more fully in perldata.
|
|
|
|
# (perldoc perldata).
|
2013-08-13 18:37:52 +04:00
|
|
|
|
2015-12-14 02:03:47 +03:00
|
|
|
#### References
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# More complex data types can be constructed using references, which
|
|
|
|
# allow you to build arrays and hashes within arrays and hashes.
|
2015-12-14 02:03:47 +03:00
|
|
|
|
|
|
|
my $array_ref = \@array;
|
|
|
|
my $hash_ref = \%hash;
|
|
|
|
my @array_of_arrays = (\@array1, \@array2, \@array3);
|
|
|
|
|
|
|
|
# You can also create anonymous arrays or hashes, returning a reference:
|
|
|
|
|
|
|
|
my $fruits = ["apple", "banana"];
|
|
|
|
my $colors = {apple => "red", banana => "yellow"};
|
|
|
|
|
|
|
|
# References can be dereferenced by prefixing the appropriate sigil.
|
|
|
|
|
|
|
|
my @fruits_array = @$fruits;
|
|
|
|
my %colors_hash = %$colors;
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# As a shortcut, the arrow operator can be used to dereference and
|
|
|
|
# access a single value.
|
2015-12-14 02:03:47 +03:00
|
|
|
|
|
|
|
my $first = $array_ref->[0];
|
|
|
|
my $value = $hash_ref->{banana};
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# See perlreftut and perlref for more in-depth documentation on
|
|
|
|
# references.
|
2013-08-13 18:37:52 +04:00
|
|
|
|
|
|
|
#### Conditional and looping constructs
|
|
|
|
|
|
|
|
# Perl has most of the usual conditional and looping constructs.
|
|
|
|
|
2015-06-10 01:00:12 +03:00
|
|
|
if ($var) {
|
|
|
|
...
|
|
|
|
} elsif ($var eq 'bar') {
|
|
|
|
...
|
2013-08-13 18:37:52 +04:00
|
|
|
} else {
|
2015-06-10 01:00:12 +03:00
|
|
|
...
|
2013-08-13 18:37:52 +04:00
|
|
|
}
|
|
|
|
|
2015-06-10 01:00:12 +03:00
|
|
|
unless (condition) {
|
|
|
|
...
|
|
|
|
}
|
2013-08-13 18:37:52 +04:00
|
|
|
# This is provided as a more readable version of "if (!condition)"
|
|
|
|
|
|
|
|
# the Perlish post-condition way
|
|
|
|
print "Yow!" if $zippy;
|
|
|
|
print "We have no bananas" unless $bananas;
|
|
|
|
|
|
|
|
# while
|
2015-06-10 01:00:12 +03:00
|
|
|
while (condition) {
|
|
|
|
...
|
|
|
|
}
|
2013-08-13 18:37:52 +04:00
|
|
|
|
|
|
|
|
2015-06-10 01:05:36 +03:00
|
|
|
# for loops and iteration
|
|
|
|
for (my $i = 0; $i < $max; $i++) {
|
|
|
|
print "index is $i";
|
|
|
|
}
|
|
|
|
|
|
|
|
for (my $i = 0; $i < @elements; $i++) {
|
|
|
|
print "Current element is " . $elements[$i];
|
2015-06-10 01:00:12 +03:00
|
|
|
}
|
2013-08-13 18:37:52 +04:00
|
|
|
|
2015-06-10 01:05:36 +03:00
|
|
|
for my $element (@elements) {
|
|
|
|
print $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
# implicitly
|
|
|
|
|
|
|
|
for (@elements) {
|
|
|
|
print;
|
2015-06-10 01:00:12 +03:00
|
|
|
}
|
2013-08-13 18:37:52 +04:00
|
|
|
|
2015-10-10 21:44:50 +03:00
|
|
|
# the Perlish post-condition way again
|
|
|
|
print for @elements;
|
2013-08-13 18:37:52 +04:00
|
|
|
|
2015-12-14 02:03:47 +03:00
|
|
|
# iterating through the keys and values of a referenced hash
|
|
|
|
print $hash_ref->{$_} for keys %$hash_ref;
|
|
|
|
|
2013-08-13 18:37:52 +04:00
|
|
|
#### Regular expressions
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# Perl's regular expression support is both broad and deep, and is the
|
|
|
|
# subject of lengthy documentation in perlrequick, perlretut, and
|
|
|
|
# elsewhere. However, in short:
|
2013-08-13 18:37:52 +04:00
|
|
|
|
|
|
|
# Simple matching
|
|
|
|
if (/foo/) { ... } # true if $_ contains "foo"
|
2016-04-15 10:23:10 +03:00
|
|
|
if ($x =~ /foo/) { ... } # true if $x contains "foo"
|
2013-08-13 18:37:52 +04:00
|
|
|
|
|
|
|
# Simple substitution
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
$x =~ s/foo/bar/; # replaces foo with bar in $x
|
|
|
|
$x =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $x
|
2013-08-13 18:37:52 +04:00
|
|
|
|
|
|
|
|
2013-08-22 06:16:54 +04:00
|
|
|
#### Files and I/O
|
|
|
|
|
|
|
|
# You can open a file for input or output using the "open()" function.
|
|
|
|
|
2017-01-21 14:06:52 +03:00
|
|
|
# For reading:
|
2013-08-22 06:16:54 +04:00
|
|
|
open(my $in, "<", "input.txt") or die "Can't open input.txt: $!";
|
2017-01-21 14:06:52 +03:00
|
|
|
# For writing (clears file if it exists):
|
2013-08-22 06:16:54 +04:00
|
|
|
open(my $out, ">", "output.txt") or die "Can't open output.txt: $!";
|
2017-01-21 14:06:52 +03:00
|
|
|
# For writing (appends to end of file):
|
2013-08-22 06:16:54 +04:00
|
|
|
open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# You can read from an open filehandle using the "<>" operator. In
|
|
|
|
# scalar context it reads a single line from the filehandle, and in list
|
|
|
|
# context it reads the whole file in, assigning each line to an element
|
|
|
|
# of the list:
|
2013-08-22 06:16:54 +04:00
|
|
|
|
|
|
|
my $line = <$in>;
|
|
|
|
my @lines = <$in>;
|
|
|
|
|
2017-01-21 14:06:52 +03:00
|
|
|
# You can write to an open filehandle using the standard "print"
|
|
|
|
# function.
|
|
|
|
|
|
|
|
print $out @lines;
|
|
|
|
print $log $msg, "\n";
|
|
|
|
|
2013-08-22 06:16:54 +04:00
|
|
|
#### Writing subroutines
|
|
|
|
|
|
|
|
# Writing subroutines is easy:
|
|
|
|
|
|
|
|
sub logger {
|
2015-06-10 01:00:12 +03:00
|
|
|
my $logmessage = shift;
|
|
|
|
|
|
|
|
open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
|
|
|
|
|
|
|
|
print $logfile $logmessage;
|
2013-08-22 06:16:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Now we can use the subroutine just as any other built-in function:
|
|
|
|
|
|
|
|
logger("We have a logger subroutine!");
|
2013-08-13 18:37:52 +04:00
|
|
|
|
2015-12-14 02:03:47 +03:00
|
|
|
#### Modules
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# A module is a set of Perl code, usually subroutines, which can be used
|
|
|
|
# in other Perl code. It is usually stored in a file with the extension
|
|
|
|
# .pm so that Perl can find it.
|
2015-12-14 02:03:47 +03:00
|
|
|
|
|
|
|
package MyModule;
|
2015-12-14 03:39:37 +03:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2015-12-14 02:03:47 +03:00
|
|
|
|
|
|
|
sub trim {
|
|
|
|
my $string = shift;
|
|
|
|
$string =~ s/^\s+//;
|
|
|
|
$string =~ s/\s+$//;
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
|
|
|
# From elsewhere:
|
|
|
|
|
|
|
|
use MyModule;
|
|
|
|
MyModule::trim($string);
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# The Exporter module can help with making subroutines exportable, so
|
|
|
|
# they can be used like this:
|
2015-12-14 02:03:47 +03:00
|
|
|
|
|
|
|
use MyModule 'trim';
|
|
|
|
trim($string);
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# Many Perl modules can be downloaded from CPAN (http://www.cpan.org/)
|
|
|
|
# and provide a range of features to help you avoid reinventing the
|
|
|
|
# wheel. A number of popular modules like Exporter are included with
|
|
|
|
# the Perl distribution itself. See perlmod for more details on modules
|
|
|
|
# in Perl.
|
2015-12-14 02:03:47 +03:00
|
|
|
|
|
|
|
#### Objects
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# Objects in Perl are just references that know which class (package)
|
|
|
|
# they belong to, so that methods (subroutines) called on it can be
|
|
|
|
# found there. The bless function is used in constructors (usually new)
|
|
|
|
# to set this up. However, you never need to call it yourself if you use
|
|
|
|
# a module like Moose or Moo (see below).
|
2015-12-14 02:03:47 +03:00
|
|
|
|
|
|
|
package MyCounter;
|
2015-12-14 03:39:37 +03:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
2015-12-14 02:03:47 +03:00
|
|
|
|
|
|
|
sub new {
|
|
|
|
my $class = shift;
|
|
|
|
my $self = {count => 0};
|
|
|
|
return bless $self, $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub count {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{count};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub increment {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{count}++;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# Methods can be called on a class or object instance with the arrow
|
|
|
|
# operator.
|
2015-12-14 02:03:47 +03:00
|
|
|
|
2015-12-14 03:51:57 +03:00
|
|
|
use MyCounter;
|
2015-12-14 02:03:47 +03:00
|
|
|
my $counter = MyCounter->new;
|
|
|
|
print $counter->count, "\n"; # 0
|
|
|
|
$counter->increment;
|
|
|
|
print $counter->count, "\n"; # 1
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# The modules Moose and Moo from CPAN can help you set up your object
|
|
|
|
# classes. They provide a constructor and simple syntax for declaring
|
|
|
|
# attributes. This class can be used equivalently to the one above.
|
2015-12-14 02:03:47 +03:00
|
|
|
|
|
|
|
package MyCounter;
|
2015-12-14 03:39:37 +03:00
|
|
|
use Moo; # imports strict and warnings
|
2015-12-14 02:03:47 +03:00
|
|
|
|
|
|
|
has 'count' => (is => 'rwp', default => 0, init_arg => undef);
|
|
|
|
|
|
|
|
sub increment {
|
|
|
|
my $self = shift;
|
|
|
|
$self->_set_count($self->count + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|
2016-04-15 10:23:10 +03:00
|
|
|
# Object-oriented programming is covered more thoroughly in perlootut,
|
|
|
|
# and its low-level implementation in Perl is covered in perlobj.
|
2015-12-14 02:03:47 +03:00
|
|
|
```
|
2013-08-13 18:37:52 +04:00
|
|
|
|
2015-12-14 02:03:47 +03:00
|
|
|
#### FAQ
|
2013-08-13 18:37:52 +04:00
|
|
|
|
2013-08-13 18:39:25 +04:00
|
|
|
perlfaq contains questions and answers related to many common tasks, and often provides suggestions for good CPAN modules to use.
|
2013-08-13 18:37:52 +04:00
|
|
|
|
|
|
|
#### Further Reading
|
|
|
|
|
2013-10-04 13:46:28 +04:00
|
|
|
- [perl-tutorial](http://perl-tutorial.org/)
|
|
|
|
- [Learn at www.perl.com](http://www.perl.org/learn.html)
|
|
|
|
- [perldoc](http://perldoc.perl.org/)
|
|
|
|
- and perl built-in : `perldoc perlintro`
|