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;

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

● 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.
Perl Arrays and Lists Software Tools. Slide 2 Lists l A list is an ordered collection of scalar data. l A list begins and ends with parentheses, with.
Perl Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
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.
9.1 Subroutines and sorting. 9.2 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { STATEMENT1; STATEMENT2;... } Subroutine.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
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.
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
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.
I/O while ($line= ){ #remove new line char \n chomp($line); if($line eq “quit”){ exit(1); } while ( ){ #remove new line char \n chomp($_); if($_ eq “quit”){
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
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.
Perl - Advanced More Advanced Perl  Functions  Control Structures  Filehandles  Process Management.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
Review of C++ Programming Part II Sheng-Fang Huang.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
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.
Set 5: Perl and Database Connections
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Introduction to Perl Yupu Liang cbio at MSKCC
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
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.
David Stotts Computer Science Department UNC Chapel Hill.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Introduction to Unix – CS 21
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
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.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Perl Subroutines User Input Perl on linux Forks and Pipes.
Perl Ed Finegan. Overview of Pearl Perl is a high-level programming language written by Larry Wall. It derives from the C programming language and to.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Subroutines Web Programming.
Context.
Perl Functions.
Lesson 2. Control structures File IO - reading and writing Subroutines
The structure of programming
Introduction to Perl Learning Objectives:
Introduction to Computing Lecture 08: Functions (Part I)
The Python interpreter
Presentation transcript:

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; statement2; statement3; } Simple Example: sub hello{ print "hello world!\n"; }

Slide 3 Defining a Function l Subroutine definitions can be anywhere in your program text (they are skipped on execution), but it is most common to put them at the end of the file. l Within the subroutine body, you may use any variable from the main program (variables in Perl are global by default). #!/usr/local/bin/perl5 -w $user = "horner"; hello(); print "goodbye $user!\n"; sub hello{ print "hello $user!\n"; }

Slide 4 Calling a Function l You can also use variables from the subroutine back in the main program (it is the same global variable): $ cat sum1 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $sum = 0; sum_a_and_b(); print "sum of $a plus $b: $sum\n"; sub sum_a_and_b{ $sum = $a + $b; } $ sum1 sum of 1 plus 2: 3 $

Slide 5 Returning Values l You can return a value from a function, and use it in any expression as in C++: $ cat sum2 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $c = sum_a_and_b() + 1; print "value of c: $c\n"; sub sum_a_and_b{ return $a + $b; } $ sum2 value of c: 4 $

Slide 6 Returning Values l A subroutine can also return a list of values: $ cat list1 #!/usr/local/bin/perl5 -w $a = 1; $b = = list_of_a_and_b(); print "list of sub list_of_a_and_b{ return ($a,$b); } $ list1 list of c: 1 2 $

Slide 7 Returning Values l Another example: $ cat max1 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $max = max_of_a_and_b(); print "max: $max\n"; sub max_of_a_and_b{ if($a > $b){ return $a; } else{ return $b; } } $ max1 max: 2 $

Slide 8 Arguments l You can also pass arguments to a subroutine. The arguments are assigned to a list in a special for the duration of the subroutine. $ cat max2 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $max = max($a, $b); print "max: $max\n"; sub max{ if($_[0] > $_[1]){ return $_[0]; } else{ return $_[1]; } } $ max2 max: 2

Slide 9 Arguments A more general way to write max() with no limit on the number of arguments: $ cat max3 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $max = max($a, $b, 5); print "max: $max\n"; sub max{ $max = 0; foreach $n if($n > $max){ $max = $n; } } return $max; } $ max3 max: 5

Slide 10 Arguments Don’t confuse $_ they are unrelated. l Excess parameters are ignored if you don’t use them. Insufficient parameters simply return undef if you look beyond the end of is local to the subroutine.

Slide 11 Local Variables You can create local versions of scalar, array and hash variables with the my() operator. $ cat max4 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $max = 0; $max1 = max($a, $b, 5); print "max1: $max1\n"; print "max : $max\n"; sub max{ my($max,$n);# local variables $max = 0; foreach $n if($n > $max){ $max = $n; } } return $max; } $ max4 max1: 5 max : 0

Slide 12 Local Variables l You can initialize local variables: $ cat max4 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $max = 0; $max1 = max($a, $b, 5); print "max1: $max1\n"; print "max : $max\n"; sub max{ my($max,$n) = (0,0); # initialized locals foreach $n if($n > $max){ $max = $n; } } return $max; } $ max4 max1: 5 max : 0

Slide 13 Local Variables You can also load local variables directly : $ cat max5 #!/usr/local/bin/perl5 -w $a = 1; $b = 2; $max = max($a, $b); print "max: $max\n"; sub max{ my($n1, $n2) if($n1 > $n2){ return $n1; } else{ return $n2; } } $ max5 max: 2

Slide 14 use strict You can force all variables to require declaration with my() by starting your program with: use strict; $ cat max5 #!/usr/local/bin/perl5 -w use strict; my $a = 1;# declare and initialize $a my $b = 2;# declare and initialize $b my $max = max($a, $b); # declare and initialize print "max: $max\n"; sub max{ my($n1, $n2) # declare locals if($n1 > $n2){ return $n1; } else{ return $n2; } } $ max5 max: 2

Slide 15 use strict use strict effectively makes all variables local. Typing mistakes are easier to catch with use strict, because you can no longer accidentally reference $billl instead of $bill. Programs also run a bit faster with use strict. For these reasons, many Perl programmers automatically begin every Perl program with use strict. l It is up to you which style you prefer.