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.

Slides:



Advertisements
Similar presentations
Procedural programming in Java
Advertisements

Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
Sub Programs To Solve a Problem, First Make It Simpler.
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;
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.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Apply Sub Procedures/Methods and User Defined Functions
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
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.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Computer Science 210 Computer Organization Introduction to Subroutines.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 05.
Chapter 2 User_defined Function. Chapter Goals In this chapter, you’ll learn all about PHP functions, including how : to create and invoke them, pass.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CS 330 Programming Languages 10 / 07 / 2008 Instructor: Michael Eckmann.
Bioinformatics Introduction to Perl. Introduction What is Perl Basic concepts in Perl syntax: – variables, strings, – Use of strict (explicit variables)
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
CPS120: Introduction to Computer Science Lecture 14 Functions.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Introduction to Unix – CS 21
Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing.
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 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.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
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.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
Information and Computer Sciences University of Hawaii, Manoa
Input from STDIN STDIN, standard input, comes from the keyboard.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Functions BIS1523 – Lecture 17.
Topics Introduction to File Input and Output
Subroutines Web Programming.
Perl Functions.
Functions In Matlab.
Introduction to Python: Day Three
G. Pullaiah College of Engineering and Technology
CISC101 Reminders All assignments are now posted.
Topics Introduction to File Input and Output
Chapter (3) - Procedures
Presentation transcript:

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 programs into a series of smaller, more manageable pieces – Making programs more readable – Making programs easier to test – Allowing for multiple programmers to work on a single program

Subroutines Recall from C – Subroutines have a name – Subroutines have an argument/parameter list for input values – Subroutines have a return type for output value(s) – Subroutine variables have local (to the subroutine) scope PERL is no different in these respects except for the last item – scope

Scope in PERL When a variable is used without declaring it, by definition it has global scope – That is, it can be used anywhere within the program $str = ‘abcde’; $regexp = ‘^ab*a$’; – Both $str and $regexp are globally scoped variables – If their contents is changed from within a subroutine, it will also change outside of the subroutine This is not how C works

Scope in PERL But, this “feature” of PERL can be turned off though the use of a special keyword/construct $str = ‘abc’; print $str, “\n”; can be changed to my($str) = ‘abc’; print $str, “\n”; to make the variable $str locally scoped to the block in which it is being used

Scope in PERL Enforcing scope and variable declaration – Also recall that in C we absolutely must declare all variables prior to usage – PERL has no such restriction Causes problems if you misspell variable names Causes problems if you use the same variable name more than once – We can make PERL act like C by including the following directive (it’s not a PERL code statement) at the top of our code file use strict;

Scope in PERL This program will not compile: use strict; $str = ‘abc’; print $str, “\n”; But this one will use strict; my($str) = ‘abc’; print $str, “\n”; Note that there is still no data typing! We’re just declaring the usage of a variable name

Back to subroutines Subroutine format sub mysubroutine { my($arg) my($localvar); # do something here return $localvar; }

Argument passing All of the previous should be fairly clear except for the line my($arg) This is how arguments are passed in – Note that there is no argument list in the subroutine definition – Arguments get lumped together as a special array called _ (underscore) – The line above pulls the arguments out of the array and places them into locally scoped scalar variables (just one in this example)

Argument passing More than one argument my($arg1, $arg2) Arguments are passed by value – Even if you alter their value within the subroutine, their value on the outside remains unchanged Passing arrays gets complicated because they all get lumped into a single array – This can be avoided by passing by reference but… – The syntax is horrible and I’m not going to burden you with it during the last week of class

Calling a subroutine Just do the obvious… $arg = ‘abc’; $retvar = mysubroutine($arg); print $retvar, “\n”; Or, better yet my($arg) = ‘abc’; my($retvar) = mysubroutine($arg); print $retvar, “\n”;

Practice Write a subroutine that takes one argument and returns it with the string “ modified by subroutine” appended – Recall the append is done with the. operator $var = $var.‘append me’; Write a subroutine that takes two arguments, one string and one regular expression, and returns 1 if they match, 0 if the do not – Read the string and the regular expression from the keyboard (don’t forget to chomp them before sending to the subroutine)

And more practice Convert your code to print out a file 30 lines at a time to use a subroutine – That is, make a subroutine that is called like this: printfile($filename); – In your main program, use a while loop to read a filename from the keyboard, then print the file – Halt the main program (while loop) when the user types a carriage-return with no filename