Perl Basics Learning Objectives: 1. To understand the commands available for control flow in Perl 2. To learn some useful operators in Perl.

Slides:



Advertisements
Similar presentations
IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
Advertisements

More Perl Control Flow Software Tools. Slide 2 Control Flow l We have already seen several Perl control flow statements: n if n while n for n last l Other.
Perl I/O Learning Objectives: 1. To understand how to perform input from standard Input & how to process the input 2. To understand how to perform input.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
4.1 Controls: Ifs and Loops. 4.2 Controls: if ? Controls allow non-sequential execution of commands, and responding to different conditions else { print.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Scalar Variables Start the file with: #! /usr/bin/perl –w No spaces or newlines before the the #! “#!” is sometimes called a “shebang”. It is a signal.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Perl Hashes Learning Objectives: 1. To understand the concept of Hash as a data structure 2. To learn the operations of Hash which are available in Perl.
Introduction to Perl Learning Objectives: 1. To introduce the features provided by Perl 2. To learn the basic Syntax & simple Input/Output control in Perl.
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Bash, part 2 Prof. Chris GauthierDickey COMP Unix Tools.
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/28.
Perl Basics Software Tools. Slide 2 Control Flow l Perl has several control flow statements: n if n while n for n unless n until n do while n do until.
Perl Hashes Learning Objectives: 1. To understand the concept of Hash as a data structure 2. To learn the operations of Hash which are available in Perl.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
More Perl Control Flow Learning Objectives: 1. To learn more commands for control flow 2. To apply the new commands learnt for writing tidier program 3.
Introduction to Perl Software Tools. Slide 2 Introduction to Perl l Perl is a scripting language that makes manipulation of text, files, and processes.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
More Perl Control Flow Learning Objectives: 1. To learn more commands for control flow 2. To apply the new commands learnt for writing tidier program 3.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
3ex.1 Note: use strict on the first line Because of a bug in the Perl Express debugger you have to put “use strict;” on the first line of your scripts.
Lab 8 Shell Script Reference:
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
The if construct The general format of the if command is: Every command has exit status If the exit status is zero, then the commands that follow between.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Perl By Gabe and Ted. History Perl was created by Larry Wall while working at NASA’s Jet Propulsion Labs. Larry Wall also created patch which is in widespread.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Perl Practical(?)‏ Extraction and Report Language.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CPTG286K Programming - Perl Chapter 4: Control Structures.
CS 105 Perl: Basic I/O, Context, Strings, Lists Nathan Clement January 22, 2014.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Programming Perl in UNIX Course Number : CIT 370 Week 3 Prof. Daniel Chen.
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Introduction to Perl NICOLE VECERE. Background General Purpose Language ◦ Procedural, Functional, and Object-oriented Developed for text manipulation.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
More Shell Programming. Slide 2 Control Flow  The shell allows several control flow statements:  if  while  for.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Chapter 17 Arrays Perl to denote an array, for = (10, 20, 30, 50); Array subscripts are number from 0. Array elements require scalar.
Chapter 5 - Control Structures: Part 2
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Control Structures: if Conditional
Control Structures: for & while Loops
Perl I/O Learning Objectives:
Chapter 4: Control Structures I (Selection)
More Shell Programming
The Selection Structure
Perl Control Flow Learning Objectives:
Introduction to Bash Programming, part 3
INTRODUCTION to PERL PART 1.
REPETITION Why Repetition?
Control Structures.
Presentation transcript:

Perl Basics Learning Objectives: 1. To understand the commands available for control flow in Perl 2. To learn some useful operators in Perl

COMP111 Lecture 10 / Slide 2 Perl Basics Table of Content  Control flow  “if” statement  “if … else “ statement  “if … elsif … else “ statement  Relational Operator  Truth in Perl  And, Or, Not  “while” statement  Looping using for  “last” command  String Operator  Variable Interpolation  Exponentiation  Operator Precedence

COMP111 Lecture 10 / Slide 3 Control Flow  Perl has several control flow statements:  if  while  for  unless  until  do while  do until  foreach

COMP111 Lecture 10 / Slide 4 “ if “ statement  The Perl if statement works almost the same as in C++: #!/usr/local/bin/perl5 -w $user = `whoami`; chomp($user); if($user eq "clinton"){ print "Hi Bill!\n"; }  The eq operator compares two strings, and returns true if they are equal (use == for numeric comparisons).  The curly braces { } are always required in Perl (even if only one statement inside, unlike C++). This avoids the “ dangling else ” problem.

COMP111 Lecture 10 / Slide 5 “ if … else “ statement  The if else statement is similar: #!/usr/local/bin/perl5 -w $user = `whoami`; chomp($user); if ($user eq "clinton") { print "Hi Bill!\n"; } else { print "Hi $user!\n"; }

COMP111 Lecture 10 / Slide 6 “ if … elsif … else “ statement  You can also handle a list of cases: #!/usr/local/bin/perl5 -w $users = `who | wc -l`; chomp($users); if ($users > 4){ print "Heavy load!\n"; } elsif ($users > 1){ print "Medium load\n"; } else { print "Just me!\n"; }

COMP111 Lecture 10 / Slide 7 Relational Operators  Perl ’ s numeric and string comparison operators: ComparisonNumericString Equal ==eq Not equal !=ne Less than <lt Greater than >gt Less than or equal to <=le Greater than or equal to >=ge

COMP111 Lecture 10 / Slide 8 Truth in Perl  Truth is flexible in Perl:  Expressions that evaluate to false 0# traditional false value ""# the null string "0"# only non-zero length false string  Some examples of truth: 1 # traditional true value 684 # non-zero numerical values are true " " # whitespace is true "hello" # strings are true "00" # a string

COMP111 Lecture 10 / Slide 9 And, Or, Not  1 represents true, and 0 false (as in C++).  You can also combine and negate expressions with logical and ( && ), logical or ( || ), and not ( ! ) just like in C++: #!/usr/local/bin/perl5 -w chomp($user = `whoami`); chomp($nme = `who | grep $user | wc -l`); chomp($nusers = `who | wc -l`); if($nusers - $nme && $user ne "clinton"){ print "Someone else is logged in!\n"; } else{ print "All is well!\n"; }

COMP111 Lecture 10 / Slide 10 “ while “ statement  The while statement loops indefinitely, while the condition is true, such as a user-controlled condition: #!/usr/local/bin/perl5 -w $resp = "no"; while($resp ne "yes"){ print "Wakeup [yes/no]? "; chomp($resp = ); } $ test11 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $

COMP111 Lecture 10 / Slide 11 Looping using for (1)  for can be used as in C++ to do incrementing loops: $ cat fac #!/usr/local/bin/perl5 -w print "Enter number: "; $n = ; $fac = 1; for($i=1; $i<=$n; $i++){ $fac *= $i; } print "The factorial of $n is $fac\n"; $ fac Enter number: 5 The factorial of 5 is 120 $ Don’t forget to chomp $n

COMP111 Lecture 10 / Slide 12  With chomp(): $ cat fac #!/usr/local/bin/perl5 -w print "Enter number: "; chomp($n = ); $fac = 1; for($i=1; $i<=$n; $i++){ $fac *= $i; } print "The factorial of $n is $fac\n"; $ fac Enter number: 5 The factorial of 5 is 120 $ Looping using for (2)

COMP111 Lecture 10 / Slide 13 “last “ command  The last command works like the C++ break command, breaking out of the innermost loop : $ cat test12 #!/usr/local/bin/perl5 -w while(1){ print "Wakeup [yes/no]? "; chomp($resp = ); if($resp eq "yes"){ last; } $ test12 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $

COMP111 Lecture 10 / Slide 14 String Operators (1)  Concatenate strings with the “. ” operator (a period). $ cat string #!/usr/local/bin/perl5 -w $name = "Bill". "Clinton"; print "$name\n"; print "Bill"."Gates"."\n"; $ string BillClinton BillGates $

COMP111 Lecture 10 / Slide 15 String Operators (2)  The string repetition operator x allows you to repeat a string several times: $ cat string1 #!/usr/local/bin/perl5 -w $name = "Bill"x3; print "$name\n"; $n = 4; print "Bill" x 2. "Gates" x $n. "\n"; print 5; print "\n"; $test = ($n+1) x 4; print "$test\n"; $ string2 BillBillBill BillBillGatesGatesGatesGates $

COMP111 Lecture 10 / Slide 16 Variable Interpolation (1)  Putting variables inside double quotes is called variable interpolation. We have seen many examples of this.  The variable name will be the longest possible variable name that makes sense at that part of the string.  Enclose the variable in a pair of curly braces if needed to override this.

COMP111 Lecture 10 / Slide 17 Variable Interpolation (2) $ cat bill1 #!/usr/local/bin/perl5 -w $bill = "trouble"; $billgates = "cheap"; print "Bill is $bill\n"; print "Bill is $billgates\n"; print "Bill is ${bill}gates\n"; print "Bill is "."$bill\n"; print "Bill is "."$bill"."\n"; $ bill1 Bill is trouble Bill is cheap Bill is troublegates Bill is trouble $

COMP111 Lecture 10 / Slide 18 Exponentiation  Perl has an exponentiation operator ** unlike C++: $ cat exp #!/usr/local/bin/perl5 -w $n = 2; $m = 3; $result = $n ** $m; print "$n raised to the $m power is $result\n"; $ exp 2 raised to the 3 power is 8 $

COMP111 Lecture 10 / Slide 19 Operator Precedence  Operator precedence is basically the same as in C++.  As in C++, you can use parentheses to override precedence, and to clarify the grouping. $ cat prec #!/usr/local/bin/perl5 -w $n = 2; $m = 3; $result = $n + 1 * $m; print "$n plus one times $m is $result\n"; $result = ($n + 1) * $m; print "$n plus one times $m is $result\n"; $ prec 2 plus one times 3 is 5 $ prec 2 plus one times 3 is 9