SlideShare a Scribd company logo
2
Most read
4
Most read
11
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

What's hot (20)

PPTX
Types of errors 2019
Osama Ghandour Geris
 
PPT
Arrays
SARITHA REDDY
 
PPTX
Arrays
RaziyasultanaShaik
 
PPTX
Array,lists and hashes in perl
sana mateen
 
PDF
Bioinformatics and BioPerl
Jason Stajich
 
PPTX
Multiple sequence alignment
Ramya S
 
PPTX
Looping Statements and Control Statements in Python
PriyankaC44
 
PPTX
String.pptx
RitikaChoudhary57
 
PPTX
Python: Polymorphism
Damian T. Gordon
 
PDF
Perl Scripting
Varadharajan Mukundan
 
PPTX
NumPy.pptx
Paras Intotech
 
PPT
Perl tutorial
Manav Prasad
 
PDF
Regression and Classification with R
Yanchang Zhao
 
PPT
Maximum parsimony
Shruthi Krishnaswamy
 
PPTX
Dynamic programming
Zohaib HUSSAIN
 
PPSX
Java String class
DrRajeshreeKhande
 
ODP
Perl Introduction
Marcos Rebelo
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPTX
Needleman-Wunsch Algorithm
ProshantaShil
 
Types of errors 2019
Osama Ghandour Geris
 
Array,lists and hashes in perl
sana mateen
 
Bioinformatics and BioPerl
Jason Stajich
 
Multiple sequence alignment
Ramya S
 
Looping Statements and Control Statements in Python
PriyankaC44
 
String.pptx
RitikaChoudhary57
 
Python: Polymorphism
Damian T. Gordon
 
Perl Scripting
Varadharajan Mukundan
 
NumPy.pptx
Paras Intotech
 
Perl tutorial
Manav Prasad
 
Regression and Classification with R
Yanchang Zhao
 
Maximum parsimony
Shruthi Krishnaswamy
 
Dynamic programming
Zohaib HUSSAIN
 
Java String class
DrRajeshreeKhande
 
Perl Introduction
Marcos Rebelo
 
Programming in c Arrays
janani thirupathi
 
Needleman-Wunsch Algorithm
ProshantaShil
 

Viewers also liked (8)

PPTX
Strings,patterns and regular expressions in perl
sana mateen
 
PPTX
Scalar expressions and control structures in perl
sana mateen
 
PPTX
Uses for scripting languages,web scripting in perl
sana mateen
 
PPTX
Reading init param
Nuha Noor
 
PPTX
Jdbc in servlets
Nuha Noor
 
PPTX
Http request and http response
Nuha Noor
 
PPTX
Using cookies and sessions
Nuha Noor
 
PPTX
Jsp elements
Nuha Noor
 
Strings,patterns and regular expressions in perl
sana mateen
 
Scalar expressions and control structures in perl
sana mateen
 
Uses for scripting languages,web scripting in perl
sana mateen
 
Reading init param
Nuha Noor
 
Jdbc in servlets
Nuha Noor
 
Http request and http response
Nuha Noor
 
Using cookies and sessions
Nuha Noor
 
Jsp elements
Nuha Noor
 
Ad

Similar to Subroutines in perl (20)

PDF
Marc’s (bio)perl course
Marc Logghe
 
PPTX
Marcs (bio)perl course
BITS
 
PPTX
Subroutines
primeteacher32
 
PDF
Scripting3
Nao Dara
 
PPTX
Data structure in perl
sana mateen
 
PDF
Cs3430 lecture 17
Tanwir Zaman
 
ODP
Introduction to Perl - Day 2
Dave Cross
 
ODP
Intermediate Perl
Dave Cross
 
PPTX
Lecture 3 Perl & FreeBSD administration
Mohammed Farrag
 
PDF
Perl_Part6
Frank Booth
 
PDF
Cs3430 lecture 16
Tanwir Zaman
 
PPT
Perl Intro 7 Subroutines
Shaun Griffith
 
PPTX
PERL PROGRAMMING LANGUAGE Basic Introduction
johnboladevice
 
PPT
LPW: Beginners Perl
Dave Cross
 
PPT
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
 
PDF
Perl intro
Swapnesh Singh
 
PPT
Introduction to perl_lists
Vamshi Santhapuri
 
PDF
PerlIntro
tutorialsruby
 
PDF
PerlIntro
tutorialsruby
 
Marc’s (bio)perl course
Marc Logghe
 
Marcs (bio)perl course
BITS
 
Subroutines
primeteacher32
 
Scripting3
Nao Dara
 
Data structure in perl
sana mateen
 
Cs3430 lecture 17
Tanwir Zaman
 
Introduction to Perl - Day 2
Dave Cross
 
Intermediate Perl
Dave Cross
 
Lecture 3 Perl & FreeBSD administration
Mohammed Farrag
 
Perl_Part6
Frank Booth
 
Cs3430 lecture 16
Tanwir Zaman
 
Perl Intro 7 Subroutines
Shaun Griffith
 
PERL PROGRAMMING LANGUAGE Basic Introduction
johnboladevice
 
LPW: Beginners Perl
Dave Cross
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
 
Perl intro
Swapnesh Singh
 
Introduction to perl_lists
Vamshi Santhapuri
 
PerlIntro
tutorialsruby
 
PerlIntro
tutorialsruby
 
Ad

More from sana mateen (20)

PPTX
Files
sana mateen
 
PPTX
PHP Variables and scopes
sana mateen
 
PPTX
Php intro
sana mateen
 
PPTX
Php and web forms
sana mateen
 
PPTX
Mail
sana mateen
 
PPTX
Files in php
sana mateen
 
PPTX
File upload php
sana mateen
 
PPTX
Regex posix
sana mateen
 
PPTX
Encryption in php
sana mateen
 
PPTX
Authentication methods
sana mateen
 
PPTX
Xml schema
sana mateen
 
PPTX
Xml dtd
sana mateen
 
PPTX
Xml dom
sana mateen
 
PPTX
Xhtml
sana mateen
 
PPTX
Intro xml
sana mateen
 
PPTX
Dom parser
sana mateen
 
PPTX
Unit 1-subroutines in perl
sana mateen
 
PPTX
Unit 1-uses for scripting languages,web scripting
sana mateen
 
PPTX
Unit 1-strings,patterns and regular expressions
sana mateen
 
PPTX
Unit 1-scalar expressions and control structures
sana mateen
 
PHP Variables and scopes
sana mateen
 
Php intro
sana mateen
 
Php and web forms
sana mateen
 
Files in php
sana mateen
 
File upload php
sana mateen
 
Regex posix
sana mateen
 
Encryption in php
sana mateen
 
Authentication methods
sana mateen
 
Xml schema
sana mateen
 
Xml dtd
sana mateen
 
Xml dom
sana mateen
 
Intro xml
sana mateen
 
Dom parser
sana mateen
 
Unit 1-subroutines in perl
sana mateen
 
Unit 1-uses for scripting languages,web scripting
sana mateen
 
Unit 1-strings,patterns and regular expressions
sana mateen
 
Unit 1-scalar expressions and control structures
sana mateen
 

Recently uploaded (20)

PDF
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PPTX
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
PPTX
Explore USA’s Best Structural And Non Structural Steel Detailing
Silicon Engineering Consultants LLC
 
PDF
Información de microsoft purview herramienta de microsoft
macarenabenitez6
 
PPTX
darshai cross section and river section analysis
muk7971
 
PPT
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
PDF
this idjfk sgfdhgdhgdbhgbgrbdrwhrgbbhtgdt
WaleedAziz7
 
PDF
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
PPTX
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
PDF
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
PDF
Artificial intelligence,WHAT IS AI ALL ABOUT AI....pdf
Himani271945
 
DOCX
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
PPTX
Diabetes diabetes diabetes diabetes jsnsmxndm
130SaniyaAbduNasir
 
PPTX
Seminar Description: YOLO v1 (You Only Look Once).pptx
abhijithpramod20002
 
PDF
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
PDF
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
PPTX
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PDF
NTPC PATRATU Summer internship report.pdf
hemant03701
 
Authentication Devices in Fog-mobile Edge Computing Environments through a Wi...
ijujournal
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
Explore USA’s Best Structural And Non Structural Steel Detailing
Silicon Engineering Consultants LLC
 
Información de microsoft purview herramienta de microsoft
macarenabenitez6
 
darshai cross section and river section analysis
muk7971
 
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
this idjfk sgfdhgdhgdbhgbgrbdrwhrgbbhtgdt
WaleedAziz7
 
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
Artificial intelligence,WHAT IS AI ALL ABOUT AI....pdf
Himani271945
 
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
Diabetes diabetes diabetes diabetes jsnsmxndm
130SaniyaAbduNasir
 
Seminar Description: YOLO v1 (You Only Look Once).pptx
abhijithpramod20002
 
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
NTPC PATRATU Summer internship report.pdf
hemant03701
 

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