30. However, only double quotes interpolate variables and special characters such as newlines (
): print("Hello, $name!
"); # interpolate $name print('Hello, $name!
'); # prints $name!
literally
31.
32. ... except inside quoted strings: print("Hello world"); # this would print with a # line break in the middle.
33.
34.
35. Alternatively, put this shebang in your script : #!/usr/bin/perl and run your executable script #> progname.pl
36. -e : allows you to define the Perl code in the command line to be executed, -E to get 5.10 #> perl -e 'print("Hello, World!
")'
37.
38. Arrays my @ animals = ('camel', 'lama'); my @ numbers = (23, 42, 69); my @ mixed = ('camel', 42, 1.23);
39. Associative Array / Hash Tables my % fruit_color = ( apple => 'red', banana => 'yellow' );
87. This is provided as a 'more readable' version of if ( not( is_valid( $value ) ) ) { ... }
88. 0, '0', '', () and undef are all false in a boolean context. All other values are true.
89.
90. The Perlish post-condition way print("Yow!") if $zippy; print("No cubes") unless $cubes;
91.
92. There's also a negated version (don't use it): until ( is_valid( $value ) ) { … }
93. You can also use while in a post-condition: print("xpto
") while condition;
94. Going throw a hash: while (my ($key,$value) = each (%ENV)){ print "$key=$value
"; }
95.
96.
97.
98.
99.
100. next LABEL : starts the next iteration of the loop
101.
102.
103. Warning: last , next , and redo don't work in this case.
104. Exercises 1 - Scalars 1) Implement the ‘Guess the lucky number’. The program shall chose a random number between 0 and 100 and ask the user for a guess. Notice the user if the guess is bigger, smaller or equal to the random number. If the guess is correct the program shall leave otherwise re-ask for a number.
105. Exercises 1 - Array 2) Create an array that contains the names of 5 students of this class. 2a) Print the array. 2b) Create a new Array shifting the elements left by one positions (element 1 goes to 0, …) and setting the first element in the last position. Print the array. 2c) Ask a user to input a number. Print the name with that index.
113. my $my_max = max( 1, 9, 3, 7 ) ; print $my_max; # prints 9
114.
115.
116.
117.
118.
119.
120.
121.
122.
123. Exercises 2 1) Create a new subroutine that calculates the Fibonacci series. Using this subroutine, do a program that receives multiple numbers as argument and prints the Fibonacci value. F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2) 1a) with presistent variable 1b) with state variable
142. my $txt = do { local (@ARGV, $/) = ($myfile); readline(); };
143.
144.
145. $ARGV has the open file, or '-' if reading STDIN .
146.
147. The __DATA__ token opens the DATA handle in whichever package is in effect at the time, so different modules can each have their own DATA filehandle, since they (presumably) have different package names.
192. At a given character position, the first alternative that allows the regex match to succeed will be the one that matches. 'cats' =~ /c | ca | cat | cats/; # matches 'c' 'cats' =~ /cats | cat | ca | c/; # matches 'cats'
193.
194. / ( ^a|b ) c/ - matches 'ac' at start of string or 'bc' anywhere.
195. /house ( cat| ) / - matches either 'housecat' or 'house'.
196. /house ( cat ( s| ) | ) / - matches either 'housecats' or 'housecat' or 'house'.
210. Switch use qw(switch say); given ($foo) { when (undef) {say '$foo is undefined'} when ('foo') {say '$foo is the str "foo"'} when (/Milan/) {say '$foo matches /Milan/'} when ([1,3,5,7,9]) { say '$foo is an odd digit'; continue ; # Fall through } when ($_ < 100) {say '$foo less than 100'} when (&check) {say 'check($foo) is true'} default {die 'what shall I do with $foo?'} }
284. Create a set of classes to represent the animal fly capabilities. Shall have two methods fly and name(get/put), the constructor receives the animal name. Consider the following rules: dog is a animal animal doesn’t fly bird is a animal bird flies penguin is a bird penguin doesn’t fly
Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including system administration, web development, network programming, code generation and more. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). Its major features are: it's easy to use. Supports both procedural and object-oriented (OO) programming. It has powerful built-in support for text processing. it has one of the world's most impressive collections of third-party modules (http://www.cpan.org).
$ perldoc -f my Check the local and our manpages as well. $ perldoc -f our $ perldoc -f local
$ perldoc strict $ perldoc warnings Good practice Start your script files with: use strict; use warnings
$ perldoc perlvar
and , or and not are also supported as operators in their own right. They're more readable than the C-style operators, but have different precedence to && and friends.
” ab” . ”cd” is ”abcd” ” ab” x 3 is ”ababab” ( 1 .. 5 ) is (1, 2, 3, 4, 5)
$ perldoc perlop Why do we have separate numeric and string comparisons? Because we don't have special variable types and Perl needs to know whether to sort numerically (where 99 is less than 100) or alphabetically (where 100 comes before 99).
$ perldoc -f each
$ perldoc perlsyn In th foreach loop the value in $var is really the value in the array, so something like $var += 2; would change the value in the @array. Using the magic variable $_ foreach ( 0 .. $max-1) { print(&quot;This element is $_
&quot;); }
Read from the STDIN my $line = <>; chomp($line); Random Number $value = int(rand(1000));
Remember: The Fibonacci series is formed by starting with 0 and 1 and then adding the latest two numbers to get the next one: fib(0) = 0 # definition fib(1) = 1 # definition fib(2) = fib(0) + fib(1) = 1 fib(3) = fib(1) + fib(2) = 2 fib(4) = fib(2) + fib(3) = 3 ...
$ perldoc -f open $ perldoc perlfunc $ perldoc perlopentut open() is documented in extravagant detail in the perlfunc manpage and the perlopentut manpage.
$ perldoc -f close When you're done with your filehandles, you should close() them. When using scalar variables, file handls will be closed automaticaly when you come out of the variable scope.
$ perldoc -f local $/ works like awk's RS variable. The value of $/ is a string, not a regex. awk has to be better for something.
The use of this variable anywhere in a program imposes a considerable performance penalty on all regular expression matches. Most punctuation names have reasonable mnemonics. Nevertheless, if you wish to use long variable names, you need only say use English; at the top of your program. For $` , $& and $' you could use $PREMATCH , $MATCH or $POSTMATCH
$perldoc -f exists The element is not autovivified if it doesn’t exist.
$ perldoc -f our An our declares the variables to be valid globals within the enclosing block, file or eval. That is, it has the same scoping rules as a my declaration, but does not create a local variable.