Merge pull request #2232 from Grinnz/perl-cleanup

[perl/en] Fix line formatting and use $x instead of $a
This commit is contained in:
ven 2016-04-15 10:40:20 +02:00
commit 5f78b30c11

View File

@ -21,9 +21,9 @@ use strict;
use warnings; use warnings;
# All perl scripts and modules should include these lines. Strict causes # All perl scripts and modules should include these lines. Strict causes
# compilation to fail in cases like misspelled variable names, and warnings # compilation to fail in cases like misspelled variable names, and
# will print warning messages in case of common pitfalls like concatenating # warnings will print warning messages in case of common pitfalls like
# to an undefined value. # concatenating to an undefined value.
#### Perl variable types #### Perl variable types
@ -47,8 +47,8 @@ my @animals = ("camel", "llama", "owl");
my @numbers = (23, 42, 69); my @numbers = (23, 42, 69);
my @mixed = ("camel", 42, 1.23); my @mixed = ("camel", 42, 1.23);
# Array elements are accessed using square brackets, with a $ to indicate # Array elements are accessed using square brackets, with a $ to
# one value will be returned. # indicate one value will be returned.
my $second = $animals[1]; my $second = $animals[1];
## Hashes ## Hashes
@ -56,7 +56,8 @@ my $second = $animals[1];
my %fruit_color = ("apple", "red", "banana", "yellow"); my %fruit_color = ("apple", "red", "banana", "yellow");
# You can use whitespace and the "=>" operator to lay them out more nicely: # You can use whitespace and the "=>" operator to lay them out more
# nicely:
my %fruit_color = ( my %fruit_color = (
apple => "red", apple => "red",
@ -71,8 +72,8 @@ my $color = $fruit_color{apple};
#### References #### References
# More complex data types can be constructed using references, which allow # More complex data types can be constructed using references, which
# you to build arrays and hashes within arrays and hashes. # allow you to build arrays and hashes within arrays and hashes.
my $array_ref = \@array; my $array_ref = \@array;
my $hash_ref = \%hash; my $hash_ref = \%hash;
@ -88,13 +89,14 @@ my $colors = {apple => "red", banana => "yellow"};
my @fruits_array = @$fruits; my @fruits_array = @$fruits;
my %colors_hash = %$colors; my %colors_hash = %$colors;
# As a shortcut, the arrow operator can be used to dereference and access a # As a shortcut, the arrow operator can be used to dereference and
# single value. # access a single value.
my $first = $array_ref->[0]; my $first = $array_ref->[0];
my $value = $hash_ref->{banana}; my $value = $hash_ref->{banana};
# See perlreftut and perlref for more in-depth documentation on references. # See perlreftut and perlref for more in-depth documentation on
# references.
#### Conditional and looping constructs #### Conditional and looping constructs
@ -150,18 +152,18 @@ print $hash_ref->{$_} for keys %$hash_ref;
#### Regular expressions #### Regular expressions
# Perl's regular expression support is both broad and deep, and is the subject # Perl's regular expression support is both broad and deep, and is the
# of lengthy documentation in perlrequick, perlretut, and elsewhere. # subject of lengthy documentation in perlrequick, perlretut, and
# However, in short: # elsewhere. However, in short:
# Simple matching # Simple matching
if (/foo/) { ... } # true if $_ contains "foo" if (/foo/) { ... } # true if $_ contains "foo"
if ($a =~ /foo/) { ... } # true if $a contains "foo" if ($x =~ /foo/) { ... } # true if $x contains "foo"
# Simple substitution # Simple substitution
$a =~ s/foo/bar/; # replaces foo with bar in $a $x =~ s/foo/bar/; # replaces foo with bar in $x
$a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a $x =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $x
#### Files and I/O #### Files and I/O
@ -172,9 +174,10 @@ 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 $out, ">", "output.txt") or die "Can't open output.txt: $!";
open(my $log, ">>", "my.log") or die "Can't open my.log: $!"; open(my $log, ">>", "my.log") or die "Can't open my.log: $!";
# You can read from an open filehandle using the "<>" operator. In scalar # You can read from an open filehandle using the "<>" operator. In
# context it reads a single line from the filehandle, and in list context it # scalar context it reads a single line from the filehandle, and in list
# reads the whole file in, assigning each line to an element of the list: # context it reads the whole file in, assigning each line to an element
# of the list:
my $line = <$in>; my $line = <$in>;
my @lines = <$in>; my @lines = <$in>;
@ -197,9 +200,9 @@ logger("We have a logger subroutine!");
#### Modules #### Modules
# A module is a set of Perl code, usually subroutines, which can be used in # A module is a set of Perl code, usually subroutines, which can be used
# other Perl code. It is usually stored in a file with the extension .pm so # in other Perl code. It is usually stored in a file with the extension
# that Perl can find it. # .pm so that Perl can find it.
package MyModule; package MyModule;
use strict; use strict;
@ -219,24 +222,25 @@ sub trim {
use MyModule; use MyModule;
MyModule::trim($string); MyModule::trim($string);
# The Exporter module can help with making subroutines exportable, so they # The Exporter module can help with making subroutines exportable, so
# can be used like this: # they can be used like this:
use MyModule 'trim'; use MyModule 'trim';
trim($string); trim($string);
# Many Perl modules can be downloaded from CPAN (http://www.cpan.org/) and # Many Perl modules can be downloaded from CPAN (http://www.cpan.org/)
# provide a range of features to help you avoid reinventing the wheel. A # and provide a range of features to help you avoid reinventing the
# number of popular modules like Exporter are included with the Perl # wheel. A number of popular modules like Exporter are included with
# distribution itself. See perlmod for more details on modules in Perl. # the Perl distribution itself. See perlmod for more details on modules
# in Perl.
#### Objects #### Objects
# Objects in Perl are just references that know which class (package) they # Objects in Perl are just references that know which class (package)
# belong to, so that methods (subroutines) called on it can be found there. # they belong to, so that methods (subroutines) called on it can be
# The bless function is used in constructors (usually new) to set this up. # found there. The bless function is used in constructors (usually new)
# However, you never need to call it yourself if you use a module like Moose # to set this up. However, you never need to call it yourself if you use
# or Moo (see below). # a module like Moose or Moo (see below).
package MyCounter; package MyCounter;
use strict; use strict;
@ -260,7 +264,8 @@ sub increment {
1; 1;
# Methods can be called on a class or object instance with the arrow operator. # Methods can be called on a class or object instance with the arrow
# operator.
use MyCounter; use MyCounter;
my $counter = MyCounter->new; my $counter = MyCounter->new;
@ -268,9 +273,9 @@ print $counter->count, "\n"; # 0
$counter->increment; $counter->increment;
print $counter->count, "\n"; # 1 print $counter->count, "\n"; # 1
# The modules Moose and Moo from CPAN can help you set up your object classes. # The modules Moose and Moo from CPAN can help you set up your object
# They provide a constructor and simple syntax for declaring attributes. This # classes. They provide a constructor and simple syntax for declaring
# class can be used equivalently to the one above. # attributes. This class can be used equivalently to the one above.
package MyCounter; package MyCounter;
use Moo; # imports strict and warnings use Moo; # imports strict and warnings
@ -284,8 +289,8 @@ sub increment {
1; 1;
# Object-oriented programming is covered more thoroughly in perlootut, and its # Object-oriented programming is covered more thoroughly in perlootut,
# low-level implementation in Perl is covered in perlobj. # and its low-level implementation in Perl is covered in perlobj.
``` ```
#### FAQ #### FAQ