2013-08-13 14:37:52 +00:00
---
name: perl
category: language
language: perl
filename: learnperl.pl
contributors:
- ["Korjavin Ivan", "http://github.com/korjavin"]
---
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
2014-04-14 18:04:44 +00:00
# Single line comments start with a number symbol.
2013-08-13 14:37:52 +00:00
#### Perl variable types
# Variables begin with the $ symbol.
# A valid variable name starts with a letter or underscore,
# followed by any number of letters, numbers, or underscores.
### Perl has three main variable types: scalars, arrays, and hashes.
## Scalars
# A scalar represents a single value:
my $animal = "camel";
my $answer = 42;
2014-06-10 17:06:08 +00:00
# Scalar values can be strings, integers or floating point numbers, and
# Perl will automatically convert between them as required.
2013-08-13 14:37:52 +00: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);
## Hashes
# A hash represents a set of key/value pairs:
my %fruit_color = ("apple", "red", "banana", "yellow");
# You can use whitespace and the "=>" operator to lay them out more nicely:
my %fruit_color = (
2015-06-09 22:00:12 +00:00
apple => "red",
banana => "yellow",
);
2014-06-10 17:06:08 +00:00
# Scalars, arrays and hashes are documented more fully in perldata.
# (perldoc perldata).
2013-08-13 14:37:52 +00:00
2014-06-10 17:06:08 +00:00
# More complex data types can be constructed using references, which allow you
# to build lists and hashes within lists and hashes.
2013-08-13 14:37:52 +00:00
#### Conditional and looping constructs
# Perl has most of the usual conditional and looping constructs.
2015-06-09 22:00:12 +00:00
if ($var) {
...
} elsif ($var eq 'bar') {
...
2013-08-13 14:37:52 +00:00
} else {
2015-06-09 22:00:12 +00:00
...
2013-08-13 14:37:52 +00:00
}
2015-06-09 22:00:12 +00:00
unless (condition) {
...
}
2013-08-13 14:37:52 +00: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-09 22:00:12 +00:00
while (condition) {
...
}
2013-08-13 14:37:52 +00:00
2015-06-09 22:05:36 +00: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-09 22:00:12 +00:00
}
2013-08-13 14:37:52 +00:00
2015-06-09 22:05:36 +00:00
for my $element (@elements) {
print $element;
}
# implicitly
for (@elements) {
print;
2015-06-09 22:00:12 +00:00
}
2013-08-13 14:37:52 +00:00
#### Regular expressions
2014-06-10 17:06:08 +00: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 14:37:52 +00:00
# Simple matching
if (/foo/) { ... } # true if $_ contains "foo"
if ($a =~ /foo/) { ... } # true if $a contains "foo"
# Simple substitution
$a =~ s/foo/bar/; # replaces foo with bar in $a
$a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a
2013-08-22 02:16:54 +00:00
#### Files and I/O
# You can open a file for input or output using the "open()" function.
open(my $in, "< ", "input.txt") or die "Can't open input.txt: $!";
open(my $out, ">", "output.txt") or die "Can't open output.txt: $!";
open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
2014-06-10 17:06:08 +00: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 02:16:54 +00:00
my $line = < $in>;
my @lines = < $in>;
#### Writing subroutines
# Writing subroutines is easy:
sub logger {
2015-06-09 22:00:12 +00:00
my $logmessage = shift;
open my $logfile, ">>", "my.log" or die "Could not open my.log: $!";
print $logfile $logmessage;
2013-08-22 02:16:54 +00:00
}
# Now we can use the subroutine just as any other built-in function:
logger("We have a logger subroutine!");
2013-08-13 14:37:52 +00:00
```
#### Using Perl modules
2013-10-04 09:44:13 +00:00
Perl modules provide a range of features to help you avoid reinventing the wheel, and can be downloaded from CPAN (http://www.cpan.org/). A number of popular modules are included with the Perl distribution itself.
2013-08-13 14:37:52 +00:00
2013-08-13 14:39:25 +00:00
perlfaq contains questions and answers related to many common tasks, and often provides suggestions for good CPAN modules to use.
2013-08-13 14:37:52 +00:00
#### Further Reading
2013-10-04 09:46:28 +00: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`