Perl - Advanced More Advanced Perl  Functions  Control Structures  Filehandles  Process Management.

Slides:



Advertisements
Similar presentations
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Advertisements

The Web Warrior Guide to Web Design Technologies
Objectives Using functions to organize PHP code
Introduction to C Programming
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.
Computer Science 1620 Loops.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
VBA Modules, Functions, Variables, and Constants
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Perl Functions Software Tools. Slide 2 Defining a Function l A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1;
Chapter 5: Loops and Files.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Introduction to C Programming
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
Chapter 5: Control Structures II (Repetition)
Lists in Python.
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
An Introduction to Unix Shell Scripting
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
Linux+ Guide to Linux Certification, Third Edition
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Controlling Execution Dong Shao, Nanjing Unviersity.
CPS120: Introduction to Computer Science Lecture 14 Functions.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
5 1 Data Files CGI/Perl Programming By Diane Zak.
JavaScript, Fourth Edition
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
CPTG286K Programming - Perl Chapter 4: Control Structures.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
Loops and Files. 5.1 The Increment and Decrement Operators.
Week Four Agenda Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Controlling Program Flow with Decision Structures.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
CPTG286K Programming - Perl Chapter 9: Misc. Control Structures Chapter 10: Filehandles & File tests.
Perl Subroutines User Input Perl on linux Forks and Pipes.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Linux Administration Working with the BASH Shell.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
User-Written Functions
Introduction To Repetition The for loop
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,
Scripts & Functions Scripts and functions are contained in .m-files
JavaScript: Control Statements.
Functions BIS1523 – Lecture 17.
Subroutines Web Programming.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Perl I/O Learning Objectives:
Presentation transcript:

Perl - Advanced More Advanced Perl  Functions  Control Structures  Filehandles  Process Management

Perl Functions  Function – Sub routine Definition  Keyword “sub” – Definition of a sub routine  Name of the routine “subname”  Block Of statements {…} sub subname { statement_1; statement_2; statement_3; } Properties  Can be anywhere in the program  They are global – There are no local subroutine definitions Example sub say_what { print "hello, $what\n"; } $what refers to a global $what shared to the rest of the program

Invoking a User Function  We invoke a user function from within an expression by following the subroutine name with parentheses.  say_what(); or  &say_what();  A subroutine can invoke another subroutine, and that subroutine can in turn invoke another subroutine, and so on.

A subroutine can return a single value A sub routine can return a list of values sub sum_of_a_and_b { return $a + $b; } sub list_of_a_and_b { return($a,$b); } Arguments In Perl, the subroutine invocation is followed by a list within parentheses, causing the list to be automatically assigned to a special variable for the duration of the subroutine. The subroutine can access this variable to determine the number of arguments and the value of those arguments: ($_[0,1,2…])  variable is private to the subroutine Return Values

Example on Arguments sub add { $sum = 0; # initialize the sum foreach $_ { $sum += $_; # add each element } return $sum; # last expression evaluated: sum of all elements }  We invoke the above function by typing add($a, $b, …) where inside the parentheses we put as many variables as we like.  variable is private to the subroutine. This also means that a subroutine can pass arguments to another subroutine without fear of losing its variable; The nested subroutine invocation gets its in the same way.

Control Structures The control expression is evaluated for a string value in scalar context (if it's already a string, no change, but if it's a number, it is converted to a string) The if/else statement The unless statement if (some expression one) { true_statement_one_1; true_statement_one_2; true_statement_one_3; } elseif (some expression two){ true_statement_two_4; true_statement_two_5; true_statement_two_6; }else{ false_statement_1; false_statement_2; false_statement_3; } Replacing if with unless is in effect saying "If the control expression is false, do...." (An unless can also have an else, just like an if.) The while Statement while (some expression) { statement_1; statement_2; statement_3; } Perl evaluates the control expression (some expression in the example). If its value is true, the body of the while statement is evaluated once. This is repeated until the control expression becomes false.

Examples of true and false interpretations Control Expression Interpretation 0converts to "0", so false 1-1computes to 0, then converts to "0", so false 1converts to "1", so true ""empty string, so false "1"not "" or "0", so true "00"not "" or "0", so true (this is weird, watch out) "0.000"also true for the same reason and warning undefevaluates to "", so false

Control Structures The Until Statement Sometimes it is easier to say "until something is true" rather than "while not this is true." Once again, Perl has the answer. Replacing the while with until yields the desired effect: until (some_expression) { statement_1; statement_2; statement_3; }Note Both the while and the until form, the body statements will be skipped entirely if the control expression is the termination value to begin with.

Control Structures The do {} while/until Statement  The while/until statement you saw in the previous section tests its condition at the top of every loop, before the loop is entered. If the condition was already false to begin with, the loop won't be executed at all.  Perl provides the do {} while statement, which is just like the regular while statement except that it doesn't test the expression until after executing the loop once. Syntax do { … } until (some expression); do { statement_1; statement_2; statement_3; } while (some expression); As with a normal while loop, you can invert the sense of the test by changing do {} while to do {} until. The expression is still tested at the bottom, but its sense is reversed.

Control Structures The for loop statement SyntaxExample for ( initial_exp; test_exp; re-init_exp ) { statement_1; statement_2; statement_3; } for ($i=0; $i<=10; $i++) { print “Incrementing by one: $i”; } This expression typically assigns an initial value to an iterator variable, but there are no restrictions on what it can contain; in fact, it may even be empty (doing nothing). Then the test_exp expression is evaluated for truth or falsehood. If the value is true, the body is executed, followed by the re-init_exp (typically, but not solely, used to increment the iterator). Perl then reevaluates the test_exp, repeating as necessary.

Control Structures The foreach statement  This statement takes a list of values and assigns them one at a time to a scalar variable, executing a block of code with each successive assignment.  If the list you are iterating over is made of real variables rather than some function returning a list value, then the variable being used for iteration is in fact an alias for each variable in the list instead of being merely a copy of the values. It means that if you change the scalar variable, you are also changing that particular element in the list that the variable is standing in for. foreach $i { statement_1; statement_2; statement_3; = (3,5,7,9); foreach $one { $one *= 3; } is now (9,15,21,27)

Filehandles  A filehandle in a Perl program is the name for an I/O connection between your Perl process and the outside world. We've already seen and used filehandles implicitly: STDIN is a filehandle, naming the connection between the Perl process and the UNIX standard input.  Filehandles are used without a special prefix character.  TIP : use ALL UPPERCASE letters in your filehandle. not only will it stand out better, but it will also guarantee that your program won't fail when a future reserved word is introduced.  Use the open function to open additional filehandles. The syntax looks like this:  open(FILEHANDLE,"somename"); where FILEHANDLE is the new filehandle and somename is the external filename (such as a file or a device)  When we are finished with a filehandle, we may close it with the close operator, like so: close(FILEHANDLE);

Filehandles writing or appending reading  To open a file for writing, use the same open function, but prefix the filename with a greater-than sign (as in the shell):  open(OUT, ">outfile");  We can now invoke the print function to write to the file ‘outfile’ like this: print OUT “text to write to the outfile”;  We can open a file for appending by using two greater-than signs for a prefix, as in: open(LOGFILE, ">>mylogfile");  Once a filehandle is open for reading, you can read lines from it just as you can read from standard input with STDIN. So, for example, to read lines from the password file: open (IN, “/etc/passwd”); while( ) { chomp($_); print "I saw $_ in the password file!\n"; }

Filehandles example Reading from a file into another open (IN, “file1”); #opening file1 for reading open (OUT, “>>file2”) #opening file2 for writing while ( ) #reading line by line from file1 until empty line occurs { print OUT “$_”; # append in file2 last line read from file1 } close (IN); #closing file1 close (OUT); #closing file2

Process Management A Perl program can launch new processes, and like most other operations, has more than one way to do so. Using the system function Examples  In its simplest form, this function hands a single string to a brand new /bin/sh shell to be executed as a command.  For the system function, the three standard files (standard input, standard output, and standard error) are inherited from the Perl process.  The argument to system can be anything you would feed /bin/sh, so multiple commands can be included, separated by semicolons or newlines.  system (“date”); # executes the date command  To put the output of the date command into a file named right_now, something like this will work just fine: system("date >right_now") || die "cannot create right_now";  system(“date; ls;”);

Using Processes as Filehandles Another way to launch a process is to create a process that looks like a filehandle We can create a process-filehandle that either captures the output from or provides input to the process InputOutput Because the process is generating output that we want to read, we make a filehandle that is open for reading, like so:  open(WHOPROC, "who|"); # open who for reading  Note the vertical bar on the right side of who. That bar tells Perl that this open is not about a filename, but rather a command to be started. Because the bar is on the right of the command, the filehandle is opened for reading, meaning that the standard output of who is going to be captured. Similarly, to open a command that expects input, we can open a process-filehandle for writing by putting the vertical bar on the left of the command, like so:  open(LPR,"|lpr -Pslatewriter");

Using Processes as Filehandles ATTENTION  Opening a process with a process-filehandle allows the command to execute in parallel with the Perl program. Saying close on the filehandle forces the Perl program to wait until the process exits. If you don't close the filehandle, the process can continue to run even beyond the execution of the Perl program.  You don't have to open just one command at a time. You can open an entire pipeline. For example, the following line starts up an ls (1) process, which pipes its output into a tail (1) process, which finally sends its output along to the WHOPR filehandle: open(WHOPR, "ls | tail -r |");

Summary of Process Operations System Function As Filehandles  It is the simplest way to create a process.  Standard input, output, and error are unaffected.  The process finishes before any more code is executed.  A simple way to get an asynchronous process  It allows the Perl program to continue before the process is complete  A command opened as a filehandle for reading inherits the standard input and standard error from the Perl program  A command opened as a filehandle for writing inherits the standard output and standard error from the Perl program.

Perl versus C  There are no type of variables  All scalars are declared with $name  Arrays are declared  Hashes are new collection of scalar data, with individual elements selected by some index value  Hashes are declared with %name  Same control structures if/elsif/else, while, do{…}while  New control structures until, unless, do{…}unless, foreach, each  Function arguments are not declared in the definition of the function. Instead are automatically imported and we can give on each function call an arbitrary number of arguments. Then they can be accessed through predefined variable  Calling system commands, regular expressions, file manipulation are very powerful on perl.

Exercises  Write a subroutine to take a numeric value from 1 to 9 as an argument and return the English name (such as one, two, or nine). If the value is out of range, return the original number as the name instead.  Write a program to read in a filename from STDIN, then open that file and display its contents with each line preceded by the filename and a colon. For example, if fred was read in, and the file fred consisted of the three lines aaa, bbb, and ccc, you would see fred: aaa, fred: bbb, and fred: ccc.  Write a program to parse the output of the date command to get the current day of the week. If the day of the week is a weekday, print get to work, otherwise print go play in a file named weekday.  Write a program that reads in a string, then prints that string and its mapped value according to the mapping presented in the following table: Input Output red apple green leaves blue ocean