Scripting Languages. Diana Trandabăț

Similar documents
CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Pathologically Eclectic Rubbish Lister

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CSC Web Programming. Introduction to JavaScript

First Java Program - Output to the Screen

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Introduction to computers and Python. Matthieu Choplin

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

CSCI 4152/6509 Natural Language Processing Lecture 6: Regular Expressions; Text Processing in Perl

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley

Programming for Engineers Introduction to C

Computers and Computation. The Modern Computer. The Operating System. The Operating System

Chapter 1 Introduction to Computers and C++ Programming

Python for Non-programmers

Perl One-Liners by Peteris Krumins

CSC105, Introduction to Computer Science I. Introduction. Perl Directions NOTE : It is also a good idea to

CSCI 4152/6509 Natural Language Processing. Perl Tutorial CSCI 4152/6509. CSCI 4152/6509, Perl Tutorial 1

KU Compilerbau - Programming Assignment

Full file at C How to Program, 6/e Multiple Choice Test Bank

Welcome to Research Computing Services training week! November 14-17, 2011

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

An Introduction to MATLAB

BASIC ELEMENTS OF A COMPUTER PROGRAM

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Beginning Perl. Mark Senn. September 11, 2007

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

Chapter 2, Part I Introduction to C Programming

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

9.2 Linux Essentials Exam Objectives

Variables, expressions and statements

C: How to Program. Week /Mar/05

S206E Lecture 19, 5/24/2016, Python an overview

PERL Bioinformatics. Nicholas E. Navin, Ph.D. Department of Genetics Department of Bioinformatics. TA: Dr. Yong Wang

Lesson 3 Transcript: Part 2 of 2 Tools & Scripting

Python Class-Lesson1 Instructor: Yao

Intermediate Programming, Spring Misha Kazhdan

Chapter 2. Lexical Elements & Operators

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

CMPS 12A Introduction to Programming Lab Assignment 7

Chapter 2 - Introduction to C Programming

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CS 115 Lecture 4. More Python; testing software. Neil Moore

Chapter 2 Getting Started with Python

Introduction to C ++

Perl for Biologists. Session 2 March 19, Constants, variables and functions. Jaroslaw Pillardy

COMP284 Practical 1 Perl (1)

Basics of Java Programming

My First iphone App. 1. Tutorial Overview

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

6.096 Introduction to C++ January (IAP) 2009

Variables and Typing

Getting Started Values, Expressions, and Statements CS GMU

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

Let's Look Back. We talked about how to create a form in HTML. Forms are one way to interact with users

Scripting Languages Course 1. Diana Trandabăț

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Introduction to C# Applications

Introduction to Programming

Should you know scanf and printf?

Review of Fundamentals

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

PHP 1. Introduction Temasek Polytechnic

CS 302: Introduction to Programming

Professor Peter Cheung EEE, Imperial College

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Lab 1 Introduction to UNIX and C

S E C T I O N O V E R V I E W

Pace University. Fundamental Concepts of CS121 1

Classnote for COMS6100

And Parallelism. Parallelism in Prolog. OR Parallelism

PHP. Interactive Web Systems

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

PHP Personal Home Page PHP: Hypertext Preprocessor (Lecture 35-37)

Introduction to Java Applications

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Variables and Constants

C# Programming Tutorial Lesson 1: Introduction to Programming

Chapter 1 & 2 Introduction to C Language

Simple Java Programming Constructs 4

COP 3014: Fall Final Study Guide. December 5, You will have an opportunity to earn 15 extra credit points.

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

cs1114 REVIEW of details test closed laptop period

Textbook. Topic 8: Files and Exceptions. Files. Types of Files

What is PERL?

Python Unit

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

C++ Support Classes (Data and Variables)

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Full file at

A First Program - Greeting.cpp

Transcription:

Scripting Languages Diana Trandabăț Master in Computational Linguistics - 1 st year 2017-2018

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

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

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.

Getting started For Windows: install 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.

Before you start using Perl 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. 4/25/2018 Perl in a Day - Introduction 6

How to write a Perl program 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 7

What is a Perl program like? #! usr/bin/perl -w # This *very* simple program prints "Hello World! print "Hello World!"; 8

What is a Perl program like? #! usr/bin/perl w # This *very* simple program prints "Hello World! print "Hello World!"; 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 ; 9

What is a Perl program like? #! usr/bin/perl w # This *very* simple program prints "Hello World! print "Hello World!"; 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 10

What is a Perl program like? #! usr/bin/perl w # This *very* simple program prints "Hello World! print "Hello World!"; 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! 11

What to do with the program? 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! 12

Exercise 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? 13

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

Variables 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 15

Variables Names in Perl: Start with a letter Contain letters, numbers, and underscores _ Case sensitive Three major types: $ Scalars (single value) @ Lists % hash tables

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)

Defining variables # We define a variable a and assign it a value of 42 $a = 42; 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 18

Defining variables # We define a variable a and assign it a value of 42 $a = 42; Variables can be assigned values String: text (character sequence) in quotes/double quotes Numbers $a = 42; $a = some text ; 19

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 4/25/2018 Perl in a Day - Variables 20

Changing variables 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 ; 21

Basic output 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 22

Define a variable Assign it a value of 15 Print it Double the value Print it again Exercise 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 23

Basic input 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!"; 24

Input, output and new lines 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!"; 25

If, else 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."; } 26

If, else 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."; } 27

Exercise 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 < and > 28

While 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."; 29

Exercise 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) ); 30

Reading files 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 = <INPUT>; $line = <>; # is just a special case 31

Writing files 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... ; 32

Reading files 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 = <INPUT>) { 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"; } } 33

Exercise 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 <number!> tag before every number 34

Great! See you next time!