diff options
Diffstat (limited to 'perl.html.markdown')
-rw-r--r-- | perl.html.markdown | 136 |
1 files changed, 91 insertions, 45 deletions
diff --git a/perl.html.markdown b/perl.html.markdown index 61e8cd0e..17a538e3 100644 --- a/perl.html.markdown +++ b/perl.html.markdown @@ -21,9 +21,9 @@ use strict; use warnings; # All perl scripts and modules should include these lines. Strict causes -# 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. +# 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. #### Perl variable types @@ -37,26 +37,50 @@ use warnings; # A scalar represents a single value: my $animal = "camel"; my $answer = 42; +my $display = "You have $answer ${animal}s.\n"; # Scalar values can be strings, integers or floating point numbers, and # Perl will automatically convert between them as required. +# Strings in single quotes are literal strings. Strings in double quotes +# will interpolate variables and escape codes like "\n" for newline. + ## Arrays # An array represents a list of values: my @animals = ("camel", "llama", "owl"); my @numbers = (23, 42, 69); my @mixed = ("camel", 42, 1.23); -# Array elements are accessed using square brackets, with a $ to indicate -# one value will be returned. +# Array elements are accessed using square brackets, with a $ to +# indicate one value will be returned. my $second = $animals[1]; +# The size of an array is retrieved by accessing the array in a scalar +# context, such as assigning it to a scalar variable or using the +# "scalar" operator. + +my $num_animals = @animals; +print "Number of numbers: ", scalar(@numbers), "\n"; + +# Arrays can also be interpolated into double-quoted strings, and the +# elements are separated by a space character by default. + +print "We have these numbers: @numbers\n"; + +# Be careful when using double quotes for strings containing symbols +# such as email addresses, as it will be interpreted as a variable. + +my @example = ('secret', 'array'); +my $oops_email = "foo@example.com"; # 'foosecret array.com' +my $ok_email = 'foo@example.com'; + ## 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: +# You can use whitespace and the "=>" operator to lay them out more +# nicely: my %fruit_color = ( apple => "red", @@ -66,13 +90,18 @@ my %fruit_color = ( # Hash elements are accessed using curly braces, again with the $ sigil. my $color = $fruit_color{apple}; +# All of the keys or values that exist in a hash can be accessed using +# the "keys" and "values" functions. +my @fruits = keys %fruit_color; +my @colors = values %fruit_color; + # Scalars, arrays and hashes are documented more fully in perldata. # (perldoc perldata). #### References -# More complex data types can be constructed using references, which allow -# you to build arrays and hashes within arrays and hashes. +# More complex data types can be constructed using references, which +# allow you to build arrays and hashes within arrays and hashes. my $array_ref = \@array; my $hash_ref = \%hash; @@ -88,13 +117,14 @@ my $colors = {apple => "red", banana => "yellow"}; my @fruits_array = @$fruits; my %colors_hash = %$colors; -# As a shortcut, the arrow operator can be used to dereference and access a -# single value. +# As a shortcut, the arrow operator can be used to dereference and +# access a single value. my $first = $array_ref->[0]; 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 @@ -124,24 +154,28 @@ while (condition) { # for loops and iteration -for (my $i = 0; $i < $max; $i++) { +for my $i (0 .. $max) { print "index is $i"; } -for (my $i = 0; $i < @elements; $i++) { - print "Current element is " . $elements[$i]; -} - for my $element (@elements) { print $element; } +map {print} @elements; + # implicitly for (@elements) { print; } +# iterating through a hash (for and foreach are equivalent) + +foreach my $key (keys %hash) { + print $key, ': ', $hash{$key}, "\n"; +} + # the Perlish post-condition way again print for @elements; @@ -150,35 +184,45 @@ print $hash_ref->{$_} for keys %$hash_ref; #### Regular expressions -# 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: +# 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: # Simple matching if (/foo/) { ... } # true if $_ contains "foo" -if ($a =~ /foo/) { ... } # true if $a contains "foo" +if ($x =~ /foo/) { ... } # true if $x 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 +$x =~ s/foo/bar/; # replaces foo with bar in $x +$x =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $x #### Files and I/O # You can open a file for input or output using the "open()" function. +# For reading: open(my $in, "<", "input.txt") or die "Can't open input.txt: $!"; +# For writing (clears file if it exists): open(my $out, ">", "output.txt") or die "Can't open output.txt: $!"; +# For writing (appends to end of file): open(my $log, ">>", "my.log") or die "Can't open my.log: $!"; -# 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: +# 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: my $line = <$in>; my @lines = <$in>; +# You can write to an open filehandle using the standard "print" +# function. + +print $out @lines; +print $log $msg, "\n"; + #### Writing subroutines # Writing subroutines is easy: @@ -197,9 +241,9 @@ logger("We have a logger subroutine!"); #### Modules -# 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. +# 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. package MyModule; use strict; @@ -219,24 +263,25 @@ sub trim { use MyModule; MyModule::trim($string); -# The Exporter module can help with making subroutines exportable, so they -# can be used like this: +# The Exporter module can help with making subroutines exportable, so +# they can be used like this: use MyModule 'trim'; trim($string); -# 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. +# 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. #### Objects -# 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). +# 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). package MyCounter; use strict; @@ -260,7 +305,8 @@ sub increment { 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; my $counter = MyCounter->new; @@ -268,9 +314,9 @@ print $counter->count, "\n"; # 0 $counter->increment; print $counter->count, "\n"; # 1 -# 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. +# 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. package MyCounter; use Moo; # imports strict and warnings @@ -284,8 +330,8 @@ sub increment { 1; -# Object-oriented programming is covered more thoroughly in perlootut, and its -# low-level implementation in Perl is covered in perlobj. +# Object-oriented programming is covered more thoroughly in perlootut, +# and its low-level implementation in Perl is covered in perlobj. ``` #### FAQ |