1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.

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

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.
1/12 Steven Leung Very Basic Perl Tricks A Few Ground Rules File I/O and Formatting Operators, Flow Control Statements Regular Expression Subroutines Hash.
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
Advanced Perl WUC Perl Poetry Variable Scope.
The Linux Operating System Lecture 6: Perl for the Systems Administrator Tonga Institute of Higher Education.
● Perl reference
References and Data Structures. References Just as in C, you can create a variable that is a reference (or pointer) to another variable. That is, it contains.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
Programming and Perl for Bioinformatics Part III.
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
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
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;
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.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
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”){
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
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.
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
2ex.1 Lists and Arrays. 2ex.2 Comments on exercises Always run your script with “ perl -w ” and take care of all warnings  submitted scripts should not.
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.
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.
11.1 Subroutines A function is a portion of code that performs a specific task. Functions Functions we've met: $newStr = substr
Intermediate PHP (2) File Input/Output & User Defined Functions.
subroutines and references
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
– Introduction to Perl 10/20/ Introduction to Perl - Lists and Arrays Introduction to Perl Session 3 · lists and arrays.
Perl Practical(?)‏ Extraction and Report Language.
1 Chapter 4 – Breaking It Up: Functions spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Chapter 9: Perl Programming Practical Extraction and Report Language 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.
5 1 Data Files CGI/Perl Programming By Diane Zak.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
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.
Perl Chapter 5 Hashes. Outside of world of Perl, know as associative arrays Also called hash tables Perl one of few languages that has hashes built-in.
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.
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,
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.
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.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Topic 3: Lists and arrays CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 3, pages 40-55, 86-91, Programming Perl 3rd edition.
Perl Scripting III Arrays and Hashes (Also known as Data Structures) Ed Lee & Suzi Lewis Genome Informatics.
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
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.
Chapter 5 Murach's JavaScript and jQuery, C1© 2012, Mike Murach & Associates, Inc.Slide 1.
Introduction to Perl: Part II
Perl Variables: Array Web Programming.
Subroutines Web Programming.
Context.
Perl Functions.
Lesson 2. Control structures File IO - reading and writing Subroutines
CSCI 431 Programming Languages Fall 2003
PERL: part II hashes, foreach control statements, and the split function By: Kevin Walton.
Basics (Cont...).
Subroutines.
Presentation transcript:

1 PERL Functions

2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different ways. Anatomy of a subroutine sub subroutineName { my # arguments are passed is the argument stack, the arguments passed &callSub2; # Calling another subroutine return # returning values. #An explicit return is not necessary, by default, the value of the # last expression is returned. } Calling Functions: $result = &subroutine $result = subroutine $result = subroutine; $result = $result = subroutine ();

3 Functions sub showArgs { $first = shift; print "$first \n"; $second = print "$second \n"; $third = shift; print "$third print } showArgs (“A”,”B”,”C”,”D”,”E”); Output: A E B C D

4 sub changeToCaps { my ($line) = $_[0]; $line =~ tr/a-z/A-Z/; return $line; } $text = "this is a test"; $text = &changeToCaps ($text); print $text; print "\n";

5 Scope of variables Global variables are declared outside of any subroutine or function and can be accessed by any part of perl program. Local variables are private to the subroutine in which they are declared. Are of two types: –Dynamic variables are defined with local keyword and are visible to any subroutines called within the subroutine in which they are declared. –Lexical variables are private variables and are seen only in the subroutine in which they are declared. They are declared with keyword my.

6 Functions $global = 100; sub one{ local $bush = "rose"; my $tree = "cherry"; two ($tree); return ++$global; } sub two { my $shrub = $bush; print $shrub; print "\n"; my $a = print "\n"; } &one; #output rose cherry

7 $one = 1234; $two = 6789; $three = 0011; $best = &Max($one,$two,$three ); print $best, "\n"; output: 6789 sub max { my $max = shift foreach $val $max = $val if $val > $max;} return $max; }

8 Functions sub fun3 { $filename = open (FHANDLE,$filename) || die "can't open the file\n"; #all the lines in the # file are copied into # the array, = ; } sub four ($$){ my ($n1,$n2) = ($_[0],$_[1]); my $sum = $n1 + $n2; return $sum; }

9 References Creating References: Examples: $data = “abc”; $a_ref = \$data;#stores the address of data in = (“one”,”two”,”three”); $b_ref = %hash = (“Mon” =>2,”Tue” => 3); $c_ref = \%hash;

10 De-referencing print $$a_ref;or print ${a_ref}; print $$b_ref[1]; or print ${b_ref}[1]; or $b_ref->[1]; print Print $$c_ref{“Mon”}; or print $c_ref->{“Mon”}; or

11 Passing References sub fun4 { my ($array1,$array2) print print = = (5,6,7); fun4 #Rewrite the function to switch arrays sub six{ my ($hashRef1) my %returnHash = (); foreach $key (keys %$hashRef1){ $returnHash{$key} = $hashRef1->{$key}; } return %returnHash; } %fruits = ("A"=>"apple","B"=>"Banana"); %retFruits = six(\%fruits);

12 Some Useful Functions Split Takes scalars and breaks them into arrays. Takes either a delimiter or a regular expression and divides the scalar on that boundary. Example 1: $line = "John Smith"; ($firstName,$lastName) = split / /,$line; print $firstName; print "\n"; print $lastName; Example 2: $line = = split (",",$line); print Example 3: $line = "A,,B,,,C"; #extra = split (",",$line); print # A B C. Example 4: $ line = = split (m",+",$line); print #A B C

13 Some Useful Functions join: joins separate strings into a single string separated by a delimiter. $fullName = join ',',$lastName,$firstName; push: Adds a list to the end of the = = ("D","E","F"); push print #A B C D E F Pop: Returns the last element of the array. $element = print $element;# F shift: Returns the first element of the array $element = print $element;# A unshift: Prepends a list to the front of the = ("X","Y","Z"); unshift print # X Y Z B C D E