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.

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

Shell Scripting Printing; Loops and Logic printf: formatted print printf is a more standardized method to print text to the screen. It can be used instead.
● Perl reference
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.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
Perl Hashes Software Tools. Slide 2 “bill” “cheap” What is a Hash? l A hash (or associative array) is like an array, where the index can be any scalar.
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 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.
Getting Started with Perl (and Excel) Biophysics 101 September 17, 2003 Griffin Weber (With material from Jon Radoff and Ivan Ovcharenko)
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Perl Basics Learning Objectives: 1. To understand the commands available for control flow in Perl 2. To learn some useful operators in Perl.
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:
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.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
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
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
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.
Sequence, Selection, Iteration The IF Statement
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
Chapter 8 JavaScript: Control Statements, Part 2
Control Structures: for & while Loops
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 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 n foreach

Slide 3 if The Perl if statement works almost the same as in C++: #!/usr/local/bin/perl5 -w $user = `whoami`; chomp($user); if($user eq "gates"){ 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.

Slide 4 if else The if else statement is similar: #!/usr/local/bin/perl5 -w $user = `whoami`; chomp($user); if ($user eq "gates") { print "Hi Bill!\n"; } else { print "Hi $user!\n"; }

Slide 5 if elsif else l 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"; }

Slide 6 Relational Operators l 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

Slide 7 Truth in Perl l Truth is flexible in Perl: n Expressions that evaluate to false 0# traditional false value ""# the null string "0"# only non-zero length false string n 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

Slide 8 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 "gates"){ print "Someone else is logged in!\n"; } else{ print "All is well!\n"; }

Slide 9 while 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 $

Slide 10 for 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

Slide 11 for 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 $

Slide 12 last 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 $

Slide 13 String Operators 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 $

Slide 14 String Operators 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"; $ string1 BillBillBill BillBillGatesGatesGatesGates $

Slide 15 Variable Interpolation l Putting variables inside double quotes is called variable interpolation. We have seen many examples of this. l 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.

Slide 16 Variable Interpolation $ 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 $

Slide 17 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 $

Slide 18 Operator Precedence l Operator precedence is basically the same as in C++. l 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 2 plus one times 3 is 9