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.

Slides:



Advertisements
Similar presentations
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
Advertisements

● Perl reference
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
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.
COS 381 Day 21. Agenda Questions?? Resources Source Code Available for examples in Text Book in Blackboard
Perl File and Directory Access Learning Objectives: 1. To learn how to change directories being accessed in a Perl program 2. To learn the Perl’s command.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
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 Software Tools. Slide 2 Defining a Function l A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1;
Subroutines sub { –#parameters are placed – –. –return; }
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.
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 File and Directory Access Learning Objectives: 1. To learn how to change directories being accessed in a Perl program 2. To learn the Perl’s command.
CSC3530 Software Technology Tutorial Two PERL Basics.
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.
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”){
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.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
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.
CSE S. Tanimoto Perl Arrays, Functions, Lists, Refs, Etc 1 Perl: Arrays, Functions, References, Etc. Arrays Slices, splices Contexts: list, scalar.
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.
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
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.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
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.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Introduction to Unix – CS 21
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)
1 More Perl Strings References Complex data structures –Multidimensional arrays Subprograms Perl OOP –Methods –Constructors and Instances –Inheritance.
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,
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
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.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Topic 3: Lists and arrays CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 3, pages 40-55, 86-91, Programming Perl 3rd edition.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
CS 142 Lecture Notes: RubySlide 1 Basic Ruby Syntax sum = 0 i = 1 while i
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Perl Subroutines User Input Perl on linux Forks and Pipes.
Introduction to Perl: Part II
Subroutines Web Programming.
Context.
Perl Functions.
Chapter 6 - Subroutines and Functions
Basics (Cont...).
Predefined Functions Revisited
Introduction to Computing Lecture 08: Functions (Part I)
Presentation transcript:

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 into functions & return value from a function 3. To understand the scope of a Perl’s program

COMP111 Lecture 18 / Slide 2  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"; } Defining a Function (1)

COMP111 Lecture 18 / Slide 3 Defining a Function (2)  Subroutine definitions can be anywhere in your program text (they are skipped on execution)  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"; }

COMP111 Lecture 18 / Slide 4 Global Variable  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 $

COMP111 Lecture 18 / Slide 5 Sorting Question Revisited  Consider a %students{$ID}{‘CGA”} from last lecture  Print out the students’ ID’s and CGA’s in order of student CGA.  Modify sort function by order of student’s CGA foreach $ID ( sort by_CGA keys(%students) ) { … } sub by_CGA { ($students{$a}{‘CGA'} $students{$b}{‘CGA'}) || ($a cmp $b); }

COMP111 Lecture 18 / Slide 6 Returning Values (1)  You can return a value from a function, and use it 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 $

COMP111 Lecture 18 / Slide 7 Returning Values (2)  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 $

COMP111 Lecture 18 / Slide 8 Returning Values (3)  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 $

COMP111 Lecture 18 / Slide 9 Arguments (1)  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

COMP111 Lecture 18 / Slide 10 Arguments (2)  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

COMP111 Lecture 18 / Slide 11 Arguments (3)  Don ’ t confuse $_ they are unrelated.  Excess parameters are ignored if you don ’ t use them.  Insufficient parameters simply return undef if you look beyond the end of array. is local to the subroutine.

COMP111 Lecture 18 / Slide 12 Local Variables (1)  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

COMP111 Lecture 18 / Slide 13 Local Variables (2)  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

COMP111 Lecture 18 / Slide 14 Local Variables (3)  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

COMP111 Lecture 18 / Slide 15  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 Keyword: use strict (1)

COMP111 Lecture 18 / Slide 16  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.  It is up to you which style you prefer. Keyword: use strict (2)

COMP111 Lecture 18 / Slide 17 More on Parameters  Variables are passed by name in Perl, i.e., parameters from the caller can be changed. $ cat swap #!/usr/bin/perl -w sub swap { ($_[0],$_[1])=($_[1],$_[0]); } ($a,$b)=(1,2); swap($a, $b); print "$a, $b print \n"; $ swap 2, 1 2 1

COMP111 Lecture 18 / Slide 18 Structures as Parameters  To pass arrays, hashes or other structures, use pointers: $ cat vsum #!/usr/bin/perl -w sub vsum{ return undef if for ($i=0; $i $i++ ) { $sum[$i] = $_[0][$i] + $_[1][$i]; } print print $ vsum Sum1: 4 6 Sum2: 6 first second } gives element count in a scalar first second } gives element count in a scalar context $_[0][$i] i-th element (cf, 2D array) [ ] returns reference of an anonymous array We cannot use ( ) here [ ] returns reference of an anonymous array We cannot use ( ) here

COMP111 Lecture 18 / Slide 19  Here is the skeleton of a program for recursive directory processing: #!/usr/local/bin/perl5 -w sub dirtree { = ; # local variable required print "Directory listing for: $_[0]\n"; foreach print "$_\n"; dirtree($_) if (-d $_); # recursion } dirtree($ARGV[0]); Recursive Directory Processing