Download presentation
Presentation is loading. Please wait.
Published byBruce Clarke Modified over 8 years ago
1
Scripting Languages Course 7 Diana Trandab ă ț Master in Computational Linguistics - 1 st year 2015-2016
2
Today’s lecture What is Perl? How to install Perl? How to write Perl progams? How to run a Perl program? – perl program.pl Scalars
3
About programming Working with algorithms Program needs to contain exact commands – (Mostly) not: Go buy some bread – But: Put on your coat and shoes, open the door, go through it, close the door, go down the stairs… A program has a certain input Processes it Produces a certain output
4
Why Perl? PERL = Practical Extraction and Report Language Easy to learn Simple syntax Open source, available for different platforms: Unix, Mac, Windows Good at manipulating text – Good at dealing with regular expressions TMTOWTDI - “There’s more than one way to do it” Extremely popular for CGI and GUI programming.
5
Getting started… For Windows: install ActivePerl http://www.activestate.com/Products/ActivePerl http://www.activestate.com/Products/ActivePerl You may use your university account (putty), and then you don’t have to install anything. Most Linux distribution come with Perl. To find out if you have it installed already, open an terminal, and write perl –v which should give you the version of Perl that you have installed on your computer.
6
11/23/2016Perl in a Day - Introduction6 · Make sure Perl exists, find out what version it is · perl -v · How do I get help? · perldoc perl (general info, TOC) · perldoc perlop (operators like +, *) · perldoc perlfunc (functions like chomp: > 200!) · perldoc perlretut (regular expressions: /ABC/) · perldoc perlreref (regular expression reference) · perldoc -f chomp (what does chomp function do?) · perldoc File::IO (find out about a Perl module) · Type q to quit when viewing help pages or space bar for next page. Before you start using Perl…
7
How to write a Perl program 7 Perl programs can be written in any text editor – Notepad, vim, even Word… – Recommended: A simple text editor with syntax highlighting Write the program code Save the file as xxx.pl –.pl extension not necessary, but useful
8
What is a Perl program like? 8 #! usr/bin/perl -w # This *very* simple program prints "Hello World!“ print "Hello World!";
9
What is a Perl program like? 9 This line is needed in Linux, not mandatory in Windows, but it does not harm, so you may leave it in your code. The -w option tells Perl to produce extra warning messages about potential dangers. This is similar to #! usr/bin/perl use warnings; White space doesn't matter in Perl. All Perl statements end in a semicolon ; #! usr/bin/perl –w # This *very* simple program prints "Hello World!“ print "Hello World!";
10
What is a Perl program like? 10 The content of a line after the # is commentary. It is ignored by the program - with the exception of the line #! usr/bin/perl What are commentaries for, then? – They are for you, and others who will have to read the code – Imaging looking at a complex program in a few months and trying to figure out what it does Write as much commentaries as you can #! usr/bin/perl –w # This *very* simple program prints "Hello World!“ print "Hello World!";
11
What is a Perl program like? 11 This is a Perl command – In this case, for printing text on the screen Every command should start at a new line – Not a Perl requirement, but crucial for readability Every command should end with a semicolon; Many commands take arguments – Here: “Hello World!” #! usr/bin/perl –w # This *very* simple program prints "Hello World!“ print "Hello World!";
12
What to do with the program? 12 Perl works from the command line Windows: Start Run… cmd Go to the directory where you saved the program – E.g.: cd C:\Perl\MyPrograms Run the program: – perl program.pl See the results of your labours!
13
Exercise 13 Create a folder for your Perl programs Open the editor of your choice and write the „Hello World“ program – The command is print „Hello World!“; – Don‘t forget the commentary! Save the program Run it! What happens if you misprint the print command?
14
More on the first program Perl is case sensitive! print is not the same as Print $bio is not the same as $Bio print is a function which prints to the screen print("Hi") is (usually) the same as print "Hi" Inside "double quotes", \n starts new line, \t prints tab A function is called with zero or more arguments Arguments are separated by commas print takes as many arguments as you give it print ""; # legal, prints nothing, not even \n print("Hi", "There"); # prints HiThere print(Hi); # illegal (calls the function Hi) print(1+1, 2+2, "\n"); # prints 24 and a newline
15
Variables 15 The „Hello World“ program always has the same output – Not a very useful program, as such We need to be able to change the output Variables are objects that can hold different values
16
Variables Names in Perl: – Start with a letter – Contain letters, numbers, and underscores “_” – Case sensitive Three major types: – $ Scalars (single value) – @ Lists – % hash tables
17
Scalars Start with a dollar sign “$” Can be of type: – Integer – Floating point – String/text – Binary data – Reference (like a pointer) Perl is not a strongly typed language (There is no necessity to declare the variable before hand)
18
Defining variables 18 To define a variable, write a dollar sign followed by the variable’s name – Names should consist of letters, numbers and the underscore – They should start with a letter – Variable names are case-sensitive! $a and $A are different variables! – Generally, a variable’s name should tell you what the variable does # We define a variable „a“ and assign it a value of „42“ $a = 42;
19
Defining variables 19 Variables can be assigned values – String: text (character sequence) in quotes/double quotes – Numbers $a = 42; $a = “some text”; # We define a variable „a“ and assign it a value of „42“ $a = 42;
20
11/23/2016Perl in a Day - Variables20 Declaring Variables Variables can also be declared with my – Tell the program there's a variable with that name – my $value = 1; – Use my the first time you use a variable – Don't have to give a value (default is "", but –w may warn) Avoid typos – use strict; will force you to declare all variables you use with my – Put this at the top of (almost) any program – Now Perl will complain if you use an undeclared variable
21
Changing variables 21 Arithmetic operations $a = 42 / 2;# division $a = 42 + 5;# addition $a = $b * 2;# multiplication $a = $a - $b;# subtraction Also useful: $a += 42;# the same as $a = $a + 42; The same for +, -, / String operations $a = “some“. “ text“;# concatenation $a = $a. “ more text“;
22
Basic output 22 We have already seen an output command – print “text“; – print $a; – print “text $a“; – print “text “. $a+$b. “ more text.“; – Special characters: \n – new line \t – tabulator
23
Exercise 23 Define a variable Assign it a value of 15 Print it Double the value Print it again Define another variable with the string „apples“ Print both variables Change the first variable to its square and the second to „pears“ Print both variables
24
Basic input 24 The <> operator returns input from the standard source (usually, the keyboard) Syntax: $a = <>; Don’t forget to tell the user what he’s supposed to enter! Try the following program: # This program asks the user for his name and greets him print "What is your name? "; $name = <>; print "Hello $name!";
25
Input, output and new lines 25 As the user input is followed by the [Enter] key, the string in $name ends in a new line The chomp function deletes the new line at the end of a string Try the following, modified program: # This program asks the user for his name and greets him print "What is your name? "; $name = <>; chomp($name); print "Hello $name!";
26
If, else 26 Until now, the course the program runs is fixed The if clause allows us to take different actions in different circumstances # Let‘s try out a conditional clause print "Please enter password: "; $password = <>; if ($password == 42) { print "Correct password! Welcome."; } else { print "Wrong password! Access denied."; }
27
If, else 27 Note: = is the assignment operator, == is the comparison operator Else is an optional operator triggering if the if condition fails # Let‘s try out a conditional clause print "Please enter password: "; $password = <>; if ($password == 42) { print "Correct password! Welcome."; } else { print "Wrong password! Access denied."; }
28
Exercise 28 Try out the password program. – Why doesn‘t it work correctly? Fix it. – Tell the user if the number he entered is too large or too small Hint: The comparison operators you’ll need are
29
While 29 What if we want to do checks until something happens? The while loop repeats commands until its criteria are met Note: in the example below, $password has no value, so it specifically doesn’t have the value 42 # Now on to a "while" loop while ($password != 42) { print "Access denied.\n"; print "Please enter password: "; $password = <>; chomp($password); } print "Correct password! Welcome.";
30
Exercise 30 Write a small game: take a number, and make the user guess it. Tell him if it‘s too high or too low. If the user gets it right, the program terminates. – If you like, you can take a random number: $random = int (rand(10) );
31
Reading files 31 What if we want to have input from a file, not from the user? Open file for reading: open(INPUT, "<file.ext"); Read a line: $line = ; $line = <>; # is just a special case
32
Writing files 32 What if we want to print to a file, not to the screen? Open file for writing: – open(OUTPUT, “>file.ext"); Write: – print OUTPUT “Some text...”;
33
Reading files 33 A program for testing e-mail addresses Note: If we want to use a special character literally, we need to escape it with a backslash In strings : " In regular expressions:. + * ^ $ and the backslash \ itself open(INPUT, "<test.txt"); while ($line = ) { chomp($line); if ($line =~ /^.+@..+\...+$/) { # testing for e-mail: x@xx.xx print "\"$line\" is a valid e-mail address.\n"; } else { print "E-mail address \" $line\" not valid.\n"; }
34
Exercise 34 Make a text file and fill it with a Wikipedia article – Count the number of definite and indefinite articles (the and a) – Count the number of numbers and digits – Insert a tag before every number
35
Great! See you next time!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.