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
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

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 Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.
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.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
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.
Perl Process Management Learning Objectives: 1. To learn the different Perl’s commands for invoking system process 2. To learn how to perform process management.
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 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”){
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
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.
Intermediate PHP (2) File Input/Output & User Defined Functions.
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.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
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.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
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)
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
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,
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.
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
CS 142 Lecture Notes: RubySlide 1 Basic Ruby Syntax sum = 0 i = 1 while i
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
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
Predefined Functions Revisited
Introduction to Computing Lecture 08: Functions (Part I)
Subroutines.
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 Perl Functions Table of Content  Defining a Functions  Calling a Function  Returning Values  Arguments  Local Variables  Keywords: use strict  More on Parameters  Structures as Parameters  Recursion – Recursive processing of directory tree

COMP111 Lecture 18 / Slide 3  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 4 Defining a Function (2)  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.  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 5 Calling a Function  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 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, Arrays are expanded like lists when they are in parameters will be (1, 2, 1, 2) instead of two arrays. Arrays are expanded like lists when they are in parameters will be (1, 2, 1, 2) instead of two arrays.

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