=head1 NAME Quick start =head1 LANGUAGE en =head1 ABSTRACT This tutorial is a quick start for non-experienced non-programmers. It tries to cover the basics and does so in a gradual manner. =head1 DESCRIPTION This tutorial is a quick start for non-experienced non-programmers. It tries to cover the basics and does so in a gradual manner. =head1 TUTORIAL =head2 Hello, world! Let's try from the simple example. This example is a common example in all programming tutorials and books, it prints "Hello, world!". Try running the following program, you should see the output. say 'Hello, world!' Here C is a function that accepts arguments and prints them out. =head3 Exercise Try fixing the following code so it prints 'Bye': say '' __TEST__ like($stdout, qr/Bye/, 'Should print Bye'); =head2 Safety belt Often programmers make mistakes that are perfecly fine from the language syntax view but have logical issues that create hard detectable bugs. In order to detect typos and various gotchas Perl provides two very useful pragmas C and C. They are recommended for use in every piece of code. All the examples in this tutorial imply these two lines, they are not shown just for the space saving. You don't have to understand the following code, just see the difference between using safety pragmas use strict; use warnings; $x += 1; $y .= 'string'; say 'ok'; and not using them no strict; no warnings; $x += 1; $y .= 'string'; say 'ok'; =head2 Numbers Numbers are integers (1, 2, 5) and floats (1.2, 1e5). There is no real difference between them. Perl treats them the same and autoconverts automatically. Try printing the following numbers: say 1; say 4.4; say 1.2e10; As you can see the statements are ended with C<;>. This is like a dot that we use to separate sentences. =head3 Arithmetic operators Basic arithmetic manipulations include: C<+> (addition), C<-> (subtraction), C<*> (multiplication), C (division), C<**> (exponent) and C<%> (modulus). say 1 + (10/5) * 3 =head4 Exercise Print out the result of 5 to the power of 6. say __TEST__ like($code, qr/\*\*/, 'Operator ** should be used'); like($stdout, qr/15625/, 'Should be 15625'); =head2 Strings Strings are declared by using single or double quotes. The difference is not so important for now. In our Hello World example the C<'Hello, world!'> was a string. say 'foo' =head3 Basic string operators and functions Strings can be concatenated (glued) using a C<.> operator. say 'foo' . 'bar' With operator C you can repeat the strings. say 'foo' x 3; Usually you would want to manipulate strings in order to get their lengths, find a symbol or a substring and so on. Basic string manipulation functions include length(), substr(), index(), rindex(). say length 'foo'; say substr 'foo', 1, 2; say index 'foo', 'o'; say rindex 'foo', 'o'; =head4 Exercise Print out the position of string C<'ball'> in string C<'Football'>. say __TEST__ like($code, qr/index/, 'Function index should be used'); like($stdout, qr/4/, 'Should be at pos 4'); =head2 Lists Lists are, well, lists of values. They are declared by using brackets. say (1, 2, 3, 5) Often you can use ranges to save some typing: say (1 .. 5) This also works on characters. =head4 Exercise Print out a list of characters from C<'b'> to C<'m'>. say __TEST__ like($stdout, qr/bcdefghijklm/, 'Should print out bcdefghijklm'); Lists of course can hold not only numbers, but also strings: say (1, 'hello', 2, 'there') Lists used inside other lists are flattened: say (1, (2, 3, 4), 5) =head2 Assignment and variables Assignment in computer programming languages is an operation of storing a value somewhere in the computer's memory that is accessed by its name. In Perl 5 there are three built-in data types: scalars, arrays and hashes (or associative arrays). Scalars can hold strings and numbers. Arrays are ordered lists of scalars where values are accessed by index. Hashes are unordered associative array where values are accessed by keys. Variables that hold scalars, arrays or hashes are prefixed with C<$>, C<@> and C<%> respectively. Variables are usually declared by using C keyword. For example: my $x = 1; say $x; =head4 Exercise Assign to a variable C string C<'Hello, world!'> and print it. my $ say __TEST__ like($stdout, qr/Hello, world!/, 'Should print "Hello, world!"') =head3 Scalars Depending on what the variable holds (a number, a string) there are different operators your can use. Let's say you want to sum two number values: my $x = 1; my $y = 2; say $x + $y; Or you want to concatenate two string values: my $x = 'Hello'; my $y = 'There'; say $x . $y If you will try to use C<+> on strings or C<.> on numbers they will be automatically converted to appropriate types. my $x = 1; my $y = '2 times'; say $x . $y; say $x + $y; As you can see in the second example the string C<'2 times'> was converted to number, which is 2. =head4 Exercise Contatenate and print the string C<'Result='> and the sum of C<42> and C<13>. my $x = ; my $y = ; say __TEST__ like($stdout, qr/55/, 'Should print "Result=55"'); =head3 Arrays Arrays can hold a list of scalars. my @array = (1, 2, 3); say @array; Basic array manipulations include getting an element by index (starting from 0), getting the last index, shifting and popping values. my @array = (1, 2, 3); # Get the third element say $array[2]; # Get the last index say $#array; push @array, 4; say @array; pop @array; say @array; shift @array; say @array; unshift @array, 0; say @array; As you probably have noticed when accessing array element we changed C<@> to C<$>, because the element of array is a scalar, and scalars are prepended with symbol C<$>. =head4 Exercise Given the array that holds list C<(1, 2, 3, 4)> print the third element. my @array = ; say __TEST__ like($stdout, qr/3/, 'Should print 3') =head3 Hashes Hash or associated arrays are unordered collections of scalars that can be accessed by a key. A key is usually a string. my %hash = ('key1', 'value1', 'key2', 'value2'); Instead of using comma for separating keys and values Perl provides a more readable operator C<< => >>, for example: my %hash = (key1 => 'value1', key2 => 'value2'); =head3 Basic hash manipulations As with arrays when accesing hash key, the variable becames a scalar, we use symbol C<$> and braces C<{}>: my %hash = (key1 => 'value1', key2 => 'value2'); say $hash{key1}; =head3 Geting all hash keys and values my %hash = (key1 => 'value1', key2 => 'value2'); say keys %hash; say values %hash; =head2 Context Context is a very important concept in Perl. There are two main contexts: scalar and list. Context usually affects how the functions and variables behave. This is close to the natural language. The most popular usage is getting an array length for example. Normally when you use array in list context it returns all its elements, but when used in scalar context it returns its length. my @array = (1, 2, 3); my @array2 = @array; # list context say @array2; my $length = @array; # scalar context say $length; =head2 Logical and comparison operators Before we introduce the corresponding Perl operators here are the basics of Boolean algebra. Boolean algebra is a variant of algebra where instead of numbers are truth values C<0> and C<1>, where C<1> is called C, and C<0> is called C. Like in a normal algebra there are operations like C<+>, C<*> etc, the basic ones are called C, C and C. As you already know in Boolean algebra we have only C and C values. That means that not only they can be used in different operations, but also the result of those operations is either C or C. Let's look at them one by one. =head3 Truth and False There are no C and C values in Perl. In Perl C is everything that is not C where C is everything that converts to C<0>: C<0> itself, C<''> (empty string), C for example. =head3 NOT C operator is a unary operator, which means it operates on one value. In Perl C operator is C. C truth table: =begin html
x !x
0 1
1 0
=end html Let's see what are the results of using this operator on various values. In the following example we add C<0> to C values so they are not converted to empty strings by C function. say !0; say !1 + 0; say !'string that converts to 1' + 0; say !''; =head3 AND C operator is a binary operator, which means it operates on two values. In Perl C operator is C<&&>. C truth table: =begin html
x y &&
0 0 0
1 0 0
0 1 0
1 1 1
=end html Let's see what are the results of using this operator on various values. In the following example we add C<0> to C values so they are not converted to empty strings by C function. say 1 && 1; say 0 && 1; say 0 && 0; say 'string' && 1; =head3 OR C operator is also a binary operator, which means it operates on two values. In Perl C operator is C<||>. C truth table: =begin html
x y ||
0 0 0
1 0 1
0 1 1
1 1 1
=end html Let's see what are the results of using this operator on various values. In the following example we add C<0> to C values so they are not converted to empty strings by C function. say 1 || 1; say 0 || 1; say 0 || 0; say 'string' || 0; =head3 Priority As in a normal algebra the operators in Boolean algebra have their priority, where different operators are evaluated earlier than others. Order of Boolean operators: ! && || =head3 Combinations C, C and C can be combined altogether. You can use also brackets to change the order of logical flow: say (1 || 0) && 1 =head4 Exercise Fix the following statement by introducing brackets so it prints empty string instead of C<1>. say !1 || 1 && 1 __TEST__ is($stdout, "\n", 'Should be an empty string'); =head3 Comparison operators Comparison operators also return C and C but are used with numbers and strings. Because Perl does not distinguish between numbers and strings there are two separate groups of comparison for numbers and strings. =begin html
==!=<<=>>=
eqneltlegtge
=end html Let's try this example: say 1 == 1; say 10 > 2; say 3 <= 3; say 'foo' ne 'bar'; =head2 Conditional statements Conditional statements allow you change the flow of the code. Conditional statements operate with Boolean values that you've learned in a previous chapter and include C and C. When the result of expression in the brackets is true than the block surrounded by curly brackets is evaluated: if (1 == 1) { say 'True'; } if (1 == 0) { say 'False'; } When you want to do something when the expression is false you can use C: if (0) { say 'True'; } else { say 'False'; } When you want to check the expression again you can use C: my $x = 1; if ($x == 0) { say 'x is zero'; } elsif ($x < 0) { say 'x is less than zero'; } else { say 'x is more than zero'; } There is also a short form for C statement: my $x = 5; say 'True' if $x > 0; C is an opposite to C where not the true value determines whether the block is evaluated but the false value. my $x = 5; say 'True' unless $x == 0; Which is the same as: my $x = 5; say 'True' if !($x == 0); As you already know in Perl the truth values is everything that is not zero, so comparizon to 0 usually is not needed: my $x = 5; say 'True' unless $x; =head3 Exercise Fix this code so it prints C<'Hello'> instead of C<'Bye'> by using logical operator and without changing C<$x> value. my $x = 0; if ($x) { say 'Hello'; } else { say 'Bye'; } __TEST__ like($code, qr/\$x = 0/, 'Should not change $x value'); like($stdout, qr/Hello/, 'Should print "Hello"'); =head2 Loops Loops are blocks that are evaluated several times. They are usually used for repetitive actions, walking through the data structure etc. =head3 For/Foreach C loop is usually used for looping through the list or array. For example: foreach my $element (1, 2, 3, 4, 5) { say $element; } You can pass an array of course: my @array = (1 .. 5); foreach my $element (@array) { say $element; } As you can see we create a special C<$element> variable that is aliased to every array element on every iteration. Beware that by changing the C<$element> value you change the actual value in the array: my @array = (1 .. 5); foreach my $element (@array) { $element *= 2; } foreach my $element (@array) { say $element; } =head4 Exercise Print only the even values from C<0> to C<10>: foreach my $element (...) { if (...) { ... } } __TEST__ like($stdout, qr/0\n2\n4\n6\n8\n10/, 'Should print even values'); =head3 While C is a more advanced loop that iterates while the expression is true. my $i = 10; while ($i > 0) { say $i; $i = $i - 1; } As soon as expression C<< $i > 0 >> becomes false the loop stops. =head4 Exercise Print only the odd values from C<0> to C<10>: my $i = ; while ($i ...) { if (...) { ... } } __TEST__ like($stdout, qr/1\n3\n5\n7\n9/, 'Should print odd values'); =head3 Getting out of the loop Often you want to terminate the loop without waiting until it finishes. You usually use the C keyword: my $i = 0; while ($i < 100) { last if $i == 10; say $i; $i = $i + 1; } This loop will not iterate C<100> times because we terminate it when C<$i> is C<10>. =head2 Default variable The most used special variable is C<$_>, which is a default scalar variable. To understand it better let's look at the examples. You're familiar with C function. It prints whatever you pass as arguments. But what happens when you don't pass any arguments? It takes data from the default C<$_> variable. $_ = 'Hello'; say; Of course you don't usually need this functionality, but it can very useful when using loops, for example: say for (1 .. 10); By default C loop sets a C<$_> variable and C prints it. Many embedded Perl functions use default variable when no arguments are passed. =head2 Subroutines Subroutines are functions that accept arguments and can return the result. Usually subroutines are used to eliminate duplication in code, make it clearer and more understandable. Let's say you want to convert 5 miles to kilometers. You would write something like this: # Convert 5 miles to kilometers say 5 * 1.609344 But what if you wanted to convert 10 miles to kilometers or any other arbitrary number? In this case we create a subroutine that we can use lately. sub miles_to_kilometers { my ($miles) = @_; return $miles * 1.609344; } say miles_to_kilometers(5); say miles_to_kilometers(10); say miles_to_kilometers(42); The subroutine needs a bit of explanation. C is called C. In Perl arguments when passed to the subroutine go the default array C<@_> (this is also a special Per variable, just like C<$_>). You can use all the array-specific functions on the default array too of course. =head3 Exercise Write and use a subroutine that converts kilometers to miles and print 4, 6, 9 kilometers converted to miles (one kilometer is 0.621371192 miles). sub kilometers_to_miles { my ... return ... } say kilometers_to_miles(4); say kilometers_to_miles(6); say kilometers_to_miles(9); __TEST__ like($stdout, qr/2.485484768\n3.728227152\n5.592340728/, 'Should print correct values') =head2 Regular Expressions Regular expressions are a big part of the Perl language. They are essential to know and use when processing texts. And that's what Perl is good for. In short, regular expressions are special patterns that when applied to strings either match them or not, capture various substrings, modify the initial substring by substituting parts of it and so on. The most common usage for regular expression is to find out if a particular string can be found in another string. my $string = 'Hello, world!'; if ($string =~ m/Hello/) { say 'We found Hello!'; } So here are two new things: C<=~> and C (C is for C). There is also an opposite C which is evaluated to C when the regular expression is not matched. my $string = 'Hello, world!'; if ($string !~ m/Bye/) { say 'No Bye was found'; } =head3 Character Classes Character classes are regular expression constructs that match exactly one character out of many. We can create character classes with the C<[]> operators. For example: my $string = 'Bye'; if ($string =~ m/[aeiou]/) { say 'Found a vowel'; } In this case, the conditional is true because one of the characters inside the character class C<[aeiou]> was found in the string. We can also indicate a range instead of writing all the characters we want to try to match: my $string = 'hello'; if ($string =~ m/[a-g]/) { say 'Found a letter between a and g in the string'; } In this example, we have indicated C<[a-g]> instead of C<[abcdefg]>. The same happens with numbers (C<[2-5]>) and uppercase letters (C<[A-Z]>). Keep in mind that, when using ranges, you always need to indicate the range in alphabetical or numerical order. For example, C<[8-3]> will not work (it will instead look for the characters I<8>, I<-> and I<3>). =head4 Exercise Modify this regular expression to detect whether there are any 'x', 'y' or 'z' letters in the sentence: my $string = "We're looking for any x, y or z"; if ($string =~ ) { say "Found an x, y or z"; } __TEST__ like($code, qr/\[.*\]/, 'Operator [] should be used'); like($stdout, qr/Found an x, y or z/, 'Should have found an x, y or z'); Other character classes are written without the C<[]>. Instead, they consist of a backslash C<\> and a letter indicating a set of characters. For example, to find out if a string contains at least one number, we can use C<\d>: my $string = 'March has 31 days'; if ($string =~ m/\d/) { say 'Found a number!'; } With C<\d> we are saying we want to find digits. We can also use C<\w> to match any I character (letters, digits and the underscore I<_>): my $string = 'This sentence is 6 words long'; if ($string =~ m/\w/) { say 'Found a word character'; } And if we want to find out if a string contains whitespaces we can use C<\s>: my $string = 'White space'; if ($string =~ m/\s/) { say 'There is a whitespace in the string'; } Whitespaces include tabulations, spaces, line feeds and carriage returns. We can use the uppercase version of the character class to match the opposite. For example, with C<\S> we match any character that is not a whitespace or the empty string: my $string = ' '; if ($string =~ m/\S/) { say 'There is at least one character that is not a whitespace'; } else { say 'There is not a non-space character in the string'; } Or with C<\D> we match any character that is not a digit: my $string = '42'; if ($string =~ m/\D/) { say 'There is at least one character that is a not a digit'; } else { say 'There is not a non-digit character in the string'; } Finally, if we want to match any character at all, we use the dot: my $string = 'Hello, World!'; if ($string =~ m/./) { say 'Found a character'; } The only time the dot doesn't match anything is when the string is empty or when it only contains a newline. =head3 Metacharacters Regular expressions can be really sophisticated. For example if we want to check if a string has C B C characters: my $string = 'Hello'; if ($string =~ m/a|o/) { say 'a or o was found'; } In this case, we use the metacharacter C<|> to indicate that we want to match I or I. Another metacharacter is C<+>, which helps us find more than one appearance of the same character: my $string = 'Hello, World!'; if ($string =~ m/l+/) { say 'Found at least one l'; } Similarly, C<*> is used to indicate that there can be 0 or more appearances of the character: my $string = 'Hello, World!'; if ($string =~ m/l*/) { say 'There may be a letter l or more in the string'; } Since it will return true if there are no matches either, this will also work: my $string = 'Hello, World!'; if ($string =~ m/j*/) { say 'There may be a letter j or more in the string'; } Finally, with C we indicate if something is found once or not at all: my $string = 'Hello, World!'; if ($string =~ m/j?/) { say 'There may be a letter j or not in the string'; } These metacharacters are better used in a bit more complex regular expressions. For example, if we want to know if a user has written their name, we can make sure there's at least one letter: my $string = 'Larry'; if ($string =~ m/[a-z]+/) { say 'The string has at least one letter, so it can be a name' } =head3 Substitutions Up until now, we have used regular expressions to C characters with the I at the beginning of the expression. But another widely used case is for substitutions with I: my $string = 'Hello, World!'; $string =~ s/Hello/Good Bye/; say $string; In this case, we first indicate that we want to do a substitution with C, then we show which word or letter we want to change, and then we write the word or letter that is written instead. =head4 Exercise Perl's motto is "There is more than one way to do it". In this exercise, change this sentence so that it prints the motto: my $string = "There is only one way to do it"; $string =~ say $string; __TEST__ like($stdout, qr/There is more than one way to do it/, "It should print Perl's motto"); =head3 Modifiers Modifiers are letters that are written at the end of the regular expression and that influence the result. For example, we can use C to do case-insensitive matching: my $string = 'Hello, World!'; if ($string =~ m/h/i) { say 'There is an h or an H in the string'; } Without the C at the end, there would be no match, since it would look only for a lowercase I. Another very common modifier is C: my $string = 'Hello, World!'; $string =~ s/l/L/g; say $string; By indicating the I we are telling the regular expression to be I and to substitute all appearences of l for L. If we don't indicate the C, the result is I, as it only substitutes the first match it finds. Modifiers can be used in substitutions and in matches alike. =head3 Anchors Anchors are special characters in regular expressions that help us fix what we are looking for to the beginning (C<^>) or the end (C<$>) of the string. For example: my $string = 'Hello, World!'; if ($string =~ m/^H/) { say 'There is an H at the beginning of the string'; } This is true because we are looking for an H in the beginning. However: my $string = 'Hello, World!'; if ($string =~ m/^o/) { say 'There is an o at the beginning of the string'; } else { say 'There is no o at the beginning of the string'; } Notice how it doesn't match in this example because, even though there are two I in the string, none of them are at the beginning. The same happens when we anchor the regular expression to the end of the string: my $string = 'Hello, World!'; if ($string =~ m/!$/) { say 'There is an ! at the end of the string'; } In this case, it matches because the last character of the string is an exclamation mark. =head4 Exercise In this exercise, create the regular expression so that the printed statement is true, using character classes and anchors: my $string = "Perl was born in 1987"; if ($string =~ ) { say "The string ends with a year"; } __TEST__ like($code, qr/\$\W/, 'The expression should use an anchor'); like($stdout, qr/The string ends with a year/, "The if condition should be true"); =head1 AUTHORS Viacheslav Tykhanovskyi, C Alba Ferrer, C