2
Most read
BY
SANA MATEEN
WHAT IS SUBROUTINE?
 A Perl subroutine or function is a group of statements that together performs a task.
You can divide up your code into separate subroutines. How you divide up your
code among different subroutines is up to you, but logically the division usually is
so each function performs a specific task.
 Perl uses the terms subroutine, method and function interchangeably.
 The simplest way for reusing code is building subroutines.
 They allow executing the same code in several places in your application, and they
allow it to be executed with different parameters.
 Define and Call a Subroutine
 The general form of a subroutine definition in Perl programming language is as
follows −
 sub subroutine_name
 { body of the subroutine
 }
 The typical way of calling that Perl subroutine is as follows −
 subroutine_name( list of arguments );
 Or
 &subroutine_name(earlier way);
Because Perl compiles your program before executing it, it doesn't matter where
you declare your subroutine.
#
# Main Code
# pseudo-code
..set variables
.
call sub1
.
call sub2
.
call sub3
.
exit program
sub 1
# code for sub 1
exit subroutine
sub 2
# code for sub 2
exit subroutine
sub 3
# code for sub 3
call sub 4
exit subroutine
sub 4
# code sub4
exit
PROGRAM(CODE) DESIGN USING
SUBROUTINES
-PSEUDO CODE
PASSING ARGUMENTS TO A SUBROUTINE
 You can pass various arguments to a subroutine like you do in any other programming
language and they can be accessed inside the function using the special array @_. Thus
the first argument to the function is in $_[0], the second is in $_[1], and so on.
 You can pass arrays and hashes as arguments like any scalar but passing more than
one array or hash normally causes them to lose their separate identities. So we will
use references to pass any array or hash.
 Let's try the following example, which takes a list of numbers and then prints their
average −
PASSING LISTS TO SUBROUTINES
 Because the @_ variable is an array, it can be used to supply lists to a subroutine.
However, because of the way in which Perl accepts and parses lists and arrays, it can
be difficult to extract the individual elements from @_. If you have to pass a list
along with other scalar arguments, then make list as the last argument as
shown below −
PASSING HASHES TO SUBROUTINES
 When you supply a hash to a subroutine or operator that accepts a list, then hash is
automatically translated into a list of key/value pairs. For example −
RETURNING VALUE FROM A SUBROUTINE
 You can return a value from subroutine like you do in any other programming language. If you are
not returning a value from a subroutine then whatever calculation is last performed will
automatically returns value.
 You can return arrays and hashes from the subroutine like any scalar but returning more than one
array or hash normally causes them to lose their separate identities. So we will use references to
return any array or hash from a function.
 Let's try the following example, which takes a list of numbers and then returns their average −
PRIVATE VARIABLES IN A SUBROUTINE
 By default, all variables in Perl are global variables, which means they
can be accessed from anywhere in the program. But you can
create private variables called lexical variables at any time with
the my operator.
 The my operator confines a variable to a particular region of code in
which it can be used and accessed. Outside that region, this variable
cannot be used or accessed. This region is called its scope. A lexical
scope is usually a block of code with a set of braces around it, such as
those defining the body of the subroutine or those marking the code
blocks of if, while, for, foreach, and evalstatements.
 Following is an example showing you how to define a single or multiple
private variables using my operator −
 sub somefunc {
 my $variable; # $variable is invisible outside somefunc()
 my ($another, @an_array, %a_hash); # declaring many variables at
once
 }
The following example distinguishes between global variable and private variable.
ADVANTAGES OF SUBROUTINES
 Saves typing → fewer lines of code →less likely to make a mistake
 re-usable
 if subroutine needs to be modified, can be changed in only one place
 other programs can use the same subroutine
 can be tested separately
 makes the overall structure of the program clearer

More Related Content

PDF
Contact management system
PPT
Interprocess communication (IPC) IN O.S
PPT
Centralised and distributed databases
PPT
Software engg. pressman_ch-9
PPT
Tokens_C
PPTX
Transfer Modes | Computer Science
PDF
tybsc it sem 5 Linux administration notes of unit 1,2,3,4,5,6 version 3
PPTX
Cache memory and virtual memory
Contact management system
Interprocess communication (IPC) IN O.S
Centralised and distributed databases
Software engg. pressman_ch-9
Tokens_C
Transfer Modes | Computer Science
tybsc it sem 5 Linux administration notes of unit 1,2,3,4,5,6 version 3
Cache memory and virtual memory

What's hot (20)

PPTX
Multi Processors And Multi Computers
PPTX
What Is Virtual DOM In React JS.pptx
PPTX
Control Unit (CU)
DOCX
Signals And Systems Lab Manual, R18 Batch
PPTX
Code Conversion in 8085 Microprocessor
PDF
An introduction to the Design of Warehouse-Scale Computers
PPTX
Ppt gsm based automatic energy meter
PPTX
Introduction to databases
PPTX
Power theft detection
PPT
Process Scheduling
PPTX
Unified Modeling Language
PDF
Operating System Lab Manual
PPTX
Bug Tracking System
PPT
Paging.ppt
PPT
parallel programming models
PPSX
DISE - OOAD Using UML
PPTX
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
PPTX
SHLD and LHLD instruction
Multi Processors And Multi Computers
What Is Virtual DOM In React JS.pptx
Control Unit (CU)
Signals And Systems Lab Manual, R18 Batch
Code Conversion in 8085 Microprocessor
An introduction to the Design of Warehouse-Scale Computers
Ppt gsm based automatic energy meter
Introduction to databases
Power theft detection
Process Scheduling
Unified Modeling Language
Operating System Lab Manual
Bug Tracking System
Paging.ppt
parallel programming models
DISE - OOAD Using UML
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
SHLD and LHLD instruction
Ad

Viewers also liked (13)

PPTX
ARM Architecture Subroutine and Flags
PPTX
00 chapter07 and_08_conversion_subroutines_force_sp13
PPTX
Unit 1-strings,patterns and regular expressions
PPTX
Unit 1-introduction to perl
PPTX
Unit 1-perl names values and variables
PPTX
Subroutine in 8051 microcontroller
PPTX
Unit 1-scalar expressions and control structures
PPTX
Unit 1-uses for scripting languages,web scripting
PPTX
Unit 1-array,lists and hashes
PPTX
Unit 1-introduction to scripts
PPTX
Stack in microprocessor 8085(presantation)
PPT
Stack and subroutine
PPTX
Role of CPU
ARM Architecture Subroutine and Flags
00 chapter07 and_08_conversion_subroutines_force_sp13
Unit 1-strings,patterns and regular expressions
Unit 1-introduction to perl
Unit 1-perl names values and variables
Subroutine in 8051 microcontroller
Unit 1-scalar expressions and control structures
Unit 1-uses for scripting languages,web scripting
Unit 1-array,lists and hashes
Unit 1-introduction to scripts
Stack in microprocessor 8085(presantation)
Stack and subroutine
Role of CPU
Ad

Similar to Unit 1-subroutines in perl (20)

PDF
Marc’s (bio)perl course
PPTX
Marcs (bio)perl course
PPTX
Subroutines
PDF
Scripting3
PPTX
Data structure in perl
PDF
Cs3430 lecture 17
ODP
Introduction to Perl - Day 2
ODP
Intermediate Perl
PPTX
Lecture 3 Perl & FreeBSD administration
PDF
Perl_Part6
ODP
Perl Introduction
PDF
Cs3430 lecture 16
PPT
Perl Intro 7 Subroutines
PPTX
PERL PROGRAMMING LANGUAGE Basic Introduction
PPT
LPW: Beginners Perl
PPT
You Can Do It! Start Using Perl to Handle Your Voyager Needs
PDF
Perl intro
PPTX
Array,lists and hashes in perl
PPT
Introduction to perl_lists
Marc’s (bio)perl course
Marcs (bio)perl course
Subroutines
Scripting3
Data structure in perl
Cs3430 lecture 17
Introduction to Perl - Day 2
Intermediate Perl
Lecture 3 Perl & FreeBSD administration
Perl_Part6
Perl Introduction
Cs3430 lecture 16
Perl Intro 7 Subroutines
PERL PROGRAMMING LANGUAGE Basic Introduction
LPW: Beginners Perl
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Perl intro
Array,lists and hashes in perl
Introduction to perl_lists

More from sana mateen (20)

PPTX
PPTX
PHP Variables and scopes
PPTX
Php intro
PPTX
Php and web forms
PPTX
PPTX
Files in php
PPTX
File upload php
PPTX
Regex posix
PPTX
Encryption in php
PPTX
Authentication methods
PPTX
Xml schema
PPTX
Xml dtd
PPTX
Xml dom
PPTX
PPTX
Intro xml
PPTX
Dom parser
PPTX
Uses for scripting languages,web scripting in perl
PPTX
Scalar expressions and control structures in perl
PPTX
Subroutines in perl
PPTX
Strings,patterns and regular expressions in perl
PHP Variables and scopes
Php intro
Php and web forms
Files in php
File upload php
Regex posix
Encryption in php
Authentication methods
Xml schema
Xml dtd
Xml dom
Intro xml
Dom parser
Uses for scripting languages,web scripting in perl
Scalar expressions and control structures in perl
Subroutines in perl
Strings,patterns and regular expressions in perl

Recently uploaded (20)

PDF
Mechanics of materials week 2 rajeshwari
PPTX
Research Writing, Mechanical Engineering
PDF
V2500 Owner and Operatore Guide for Airbus
PDF
Software defined netwoks is useful to learn NFV and virtual Lans
PPTX
MODULE 02 - CLOUD COMPUTING-Virtual Machines and Virtualization of Clusters a...
PPTX
Unit IImachinemachinetoolopeartions.pptx
PDF
Artificial Intelligence_ Basics .Artificial Intelligence_ Basics .
DOCX
ENVIRONMENTAL PROTECTION AND MANAGEMENT (18CVL756)
PPTX
DATA STRCUTURE LABORATORY -BCSL305(PRG1)
PPTX
Software-Development-Life-Cycle-SDLC.pptx
PDF
Module 1 part 1.pdf engineering notes s7
PPTX
Solar energy pdf of gitam songa hemant k
PDF
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
PPTX
Design ,Art Across Digital Realities and eXtended Reality
PDF
Using Technology to Foster Innovative Teaching Practices (www.kiu.ac.ug)
PDF
MACCAFERRY GUIA GAVIONES TERRAPLENES EN ESPAÑOL
PDF
ASPEN PLUS USER GUIDE - PROCESS SIMULATIONS
DOCX
An investigation of the use of recycled crumb rubber as a partial replacement...
PPTX
Real Estate Management PART 1.pptxFFFFFFFFFFFFF
PPT
UNIT-I Machine Learning Essentials for 2nd years
Mechanics of materials week 2 rajeshwari
Research Writing, Mechanical Engineering
V2500 Owner and Operatore Guide for Airbus
Software defined netwoks is useful to learn NFV and virtual Lans
MODULE 02 - CLOUD COMPUTING-Virtual Machines and Virtualization of Clusters a...
Unit IImachinemachinetoolopeartions.pptx
Artificial Intelligence_ Basics .Artificial Intelligence_ Basics .
ENVIRONMENTAL PROTECTION AND MANAGEMENT (18CVL756)
DATA STRCUTURE LABORATORY -BCSL305(PRG1)
Software-Development-Life-Cycle-SDLC.pptx
Module 1 part 1.pdf engineering notes s7
Solar energy pdf of gitam songa hemant k
LS-6-Digital-Literacy (1) K12 CURRICULUM .pdf
Design ,Art Across Digital Realities and eXtended Reality
Using Technology to Foster Innovative Teaching Practices (www.kiu.ac.ug)
MACCAFERRY GUIA GAVIONES TERRAPLENES EN ESPAÑOL
ASPEN PLUS USER GUIDE - PROCESS SIMULATIONS
An investigation of the use of recycled crumb rubber as a partial replacement...
Real Estate Management PART 1.pptxFFFFFFFFFFFFF
UNIT-I Machine Learning Essentials for 2nd years

Unit 1-subroutines in perl

  • 2. WHAT IS SUBROUTINE?  A Perl subroutine or function is a group of statements that together performs a task. You can divide up your code into separate subroutines. How you divide up your code among different subroutines is up to you, but logically the division usually is so each function performs a specific task.  Perl uses the terms subroutine, method and function interchangeably.  The simplest way for reusing code is building subroutines.  They allow executing the same code in several places in your application, and they allow it to be executed with different parameters.  Define and Call a Subroutine  The general form of a subroutine definition in Perl programming language is as follows −  sub subroutine_name  { body of the subroutine  }  The typical way of calling that Perl subroutine is as follows −  subroutine_name( list of arguments );  Or  &subroutine_name(earlier way);
  • 3. Because Perl compiles your program before executing it, it doesn't matter where you declare your subroutine.
  • 4. # # Main Code # pseudo-code ..set variables . call sub1 . call sub2 . call sub3 . exit program sub 1 # code for sub 1 exit subroutine sub 2 # code for sub 2 exit subroutine sub 3 # code for sub 3 call sub 4 exit subroutine sub 4 # code sub4 exit PROGRAM(CODE) DESIGN USING SUBROUTINES -PSEUDO CODE
  • 5. PASSING ARGUMENTS TO A SUBROUTINE  You can pass various arguments to a subroutine like you do in any other programming language and they can be accessed inside the function using the special array @_. Thus the first argument to the function is in $_[0], the second is in $_[1], and so on.  You can pass arrays and hashes as arguments like any scalar but passing more than one array or hash normally causes them to lose their separate identities. So we will use references to pass any array or hash.  Let's try the following example, which takes a list of numbers and then prints their average −
  • 6. PASSING LISTS TO SUBROUTINES  Because the @_ variable is an array, it can be used to supply lists to a subroutine. However, because of the way in which Perl accepts and parses lists and arrays, it can be difficult to extract the individual elements from @_. If you have to pass a list along with other scalar arguments, then make list as the last argument as shown below −
  • 7. PASSING HASHES TO SUBROUTINES  When you supply a hash to a subroutine or operator that accepts a list, then hash is automatically translated into a list of key/value pairs. For example −
  • 8. RETURNING VALUE FROM A SUBROUTINE  You can return a value from subroutine like you do in any other programming language. If you are not returning a value from a subroutine then whatever calculation is last performed will automatically returns value.  You can return arrays and hashes from the subroutine like any scalar but returning more than one array or hash normally causes them to lose their separate identities. So we will use references to return any array or hash from a function.  Let's try the following example, which takes a list of numbers and then returns their average −
  • 9. PRIVATE VARIABLES IN A SUBROUTINE  By default, all variables in Perl are global variables, which means they can be accessed from anywhere in the program. But you can create private variables called lexical variables at any time with the my operator.  The my operator confines a variable to a particular region of code in which it can be used and accessed. Outside that region, this variable cannot be used or accessed. This region is called its scope. A lexical scope is usually a block of code with a set of braces around it, such as those defining the body of the subroutine or those marking the code blocks of if, while, for, foreach, and evalstatements.  Following is an example showing you how to define a single or multiple private variables using my operator −  sub somefunc {  my $variable; # $variable is invisible outside somefunc()  my ($another, @an_array, %a_hash); # declaring many variables at once  }
  • 10. The following example distinguishes between global variable and private variable.
  • 11. ADVANTAGES OF SUBROUTINES  Saves typing → fewer lines of code →less likely to make a mistake  re-usable  if subroutine needs to be modified, can be changed in only one place  other programs can use the same subroutine  can be tested separately  makes the overall structure of the program clearer