Perl Functions - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Perl Functions

Description:

To learn how to create functions in a Perl's program & how to call them ... You can create local versions of scalar, array and hash variables with the my() operator. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 20
Provided by: andrew184
Category:

less

Transcript and Presenter's Notes

Title: Perl Functions


1
Perl Functions
  • Learning Objectives
  • To learn how to create functions in a Perls
    program how to call them
  • To learn how to pass structured arguments into
    functions return value from a function
  • To understand the scope of a Perls program

2
Defining a Function (1)
  • 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"

3
Defining a Function (2)
  • Subroutine definitions can be anywhere in your
    program text (they are skipped on execution)
  • 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"

4
Global Variable
  • 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

5
Sorting Question Revisited
  • Consider a studentsIDCGA from last
    lecture
  • Print out the students IDs and CGAs in order
    of student CGA.
  • Modify sort function by order of students CGA

foreach ID ( sort by_CGA keys(students) )
sub by_CGA (studentsaCGA'
ltgt studentsbCGA') (a cmp b)
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

7
Returning Values (2)
  • A subroutine can also return a list of values
  • cat list1
  • !/usr/local/bin/perl5 -w
  • a 1
  • b 2
  • _at_c list_of_a_and_b()
  • print "list of c _at_c\n"
  • sub list_of_a_and_b
  • return (a,b)
  • list1
  • list of c 1 2

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 gt b) return a
  • else return b
  • max1
  • max 2

9
Arguments (1)
  • You can also pass arguments to a subroutine.
  • The arguments are assigned to a list in a special
    variable _at__ 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 gt _1) return _0
  • else return _1
  • max2
  • max 2

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 (_at__)
  • if(n gt max) max n
  • return max
  • max3
  • max 5

11
Arguments (3)
  • Dont confuse _ and _at__, they are unrelated.
  • Excess parameters are ignored if you dont use
    them.
  • Insufficient parameters simply return undef if
    you look beyond the end of the _at__ array.
  • _at__ is local to the subroutine.

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 (_at__)
  • if(n gt max) max n
  • return max
  • max4
  • max1 5
  • max 0

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 (_at__)
  • if(n gt max) max n
  • return max
  • max4
  • max1 5
  • max 0

14
Local Variables (3)
  • You can also load local variables directly from
    _at__
  • cat max5
  • !/usr/local/bin/perl5 -w
  • a 1
  • b 2
  • max max(a, b)
  • print "max max\n"
  • sub max
  • my(n1, n2) _at__
  • if(n1 gt n2) return n1
  • else return n2
  • max5
  • max 2

15
Keyword use strict (1)
  • 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) _at__ declare locals from _at__
  • if(n1 gt n2) return n1
  • else return n2
  • max5
  • max 2

16
Keyword use strict (2)
  • 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.

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 \n"
  • _at_x(1,2)
  • swap(_at_x) print "_at_x \n"
  • swap
  • 2, 1
  • 2 1

18
Structures as Parameters
  • To pass arrays, hashes or other structures, use
    pointers
  • cat vsum
  • !/usr/bin/perl -w
  • sub vsum
  • return undef if (_at__0 ! _at__1)
  • for (i0 i lt _at__0 i )
  • sumi _0i _1i
  • return _at_sum
  • _at_a(1,2) _at_b(3,4) _at_sum vsum(\_at_a, \_at_b)
  • print "Sum1 _at_sum "
  • _at_sum2vsum(2,3,4,5)
  • print "Sum2 _at_sum2\n"
  • vsum
  • Sum1 4 6 Sum2 6 8

_at__0 first array, _at__1 second array _at_
gives element count in a scalar context
_0i i-th element (cf, 2D array)
returns reference of an anonymous array We
cannot use ( ) here
19
Recursive Directory Processing
  • Here is the skeleton of a program for recursive
    directory processing
  • !/usr/local/bin/perl5 -w
  • sub dirtree
  • my _at_files lt_0/gt local variable
    required
  • print "Directory listing for _0\n"
  • foreach (_at_files)
  • print "_\n"
  • dirtree(_) if (-d _) recursion
  • dirtree(ARGV0)
Write a Comment
User Comments (0)
About PowerShow.com