perl functions and subroutines

Upload: suneeldv

Post on 04-Jun-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Perl Functions and Subroutines

    1/14

    Title

    Built-in Functions and

    Subroutines

    PERL

  • 8/13/2019 Perl Functions and Subroutines

    2/14

    Agenda

    Functions

    Built-in Functions

    Built-in Functions by Category

    Subroutines

    Defining Subroutines

    Returning Values

    Using Arguments

    Passing Arrays and Hashes by ReferenceHandling return Values

  • 8/13/2019 Perl Functions and Subroutines

    3/14

    Built-in Functions

    Terms in an expression along with literals andvariables

    Can be used either with or without parenthesesaround their arguments

  • 8/13/2019 Perl Functions and Subroutines

    4/14

    Built in Functions by Category

    Text processing related built-in functions

    chomp();#used for removing line terminators

    Example:-

    chomp($_);#removes new line character from current lineFor arrays, hashes and strings

    chop();#used for chopping of one character from string end

    Example:-

    $name = Prashanth;

    for($i=1;$i

  • 8/13/2019 Perl Functions and Subroutines

    5/14

    Continued

    length();#Gets length of string

    Example:-

    length($string);

    split ();

    Example:-

    while () {

    chomp; # avoid \n on last field

    @array = split /:/ , $_; # split current line delimited by :

    #& push them in array @array

    }

    Push, pop, shift, unshift();

    Example:-

    Push(@array1, @array2) is better than @array1 = (@array1, @array2)

  • 8/13/2019 Perl Functions and Subroutines

    6/14

    Built-in Functions Partial List

    Functions for SCALARs or stringschomp, chop, chr, crypt, hex, index, lc, lcfirst, length, oct, ord, reverse, rindex,

    sprintf, substr, tr///, uc, ucfirst,

    Regular expressions and pattern matching

    m//, pos, quotemeta, s///, split, study, qr//

    Numeric functionsabs, atan2, cos, exp, hex, int, log, oct, rand, sin, sqrt, srand

    Functions for real @ARRAYs

    pop, push, shift, splice, unshift

    Functions for list data

    grep, join, reverse, sort

    Functions for real %HASHes

    delete, each, exists, keys, values

  • 8/13/2019 Perl Functions and Subroutines

    7/14

    Built-in Functions (cont.)

    Input and output functions

    binmode, close, closedir, dbmclose, dbmopen, die, eof, fileno, flock, format, getc,print, printf, read, readdir, rewinddir, seek, seekdir, select, syscall, sysread,sysseek, syswrite, tell, telldir, truncate, warn, write

    Functions for filehandles, files, or directories

    -X, chdir, chmod, chown, chroot, fcntl, glob, ioctl,

    link, lstat, mkdir, open, opendir, readlink, rename,

    rmdir, stat, symlink, umask, unlink, utime

    Keywords related to the control flow of your perl program

    caller, continue, die, do, dump, eval, exit, goto, last, next, redo, return, sub

    Keywords related to perl modules

    do, import, no, package, require, use

    Functions for processes and process groups

    alarm, exec, fork, getpgrp, getppid, getpriority, kill, pipe, qx/STRING/, setpgrp,setpriority, sleep, system, times, wait, waitpid

  • 8/13/2019 Perl Functions and Subroutines

    8/14

    Use the chop() function in a program. Printboth the returned character and the stringthat was passed as a parameter.

    Write a program that shows what the shift()and unshift() functions do.

    Modify the program to show what the push()and pop() functions do.

  • 8/13/2019 Perl Functions and Subroutines

    9/14

    Subroutines

    User written functions

    Are called like functions

    Makes code clean

    Code reuse

    Called in program as

    &sub_name Old methodsub_name(); New method

  • 8/13/2019 Perl Functions and Subroutines

    10/14

    Defining Subroutines

    Use the keyword subfollowed by name

    followed by a code block

    Exampleprint I am not in subroutine\n;

    print Enter Your Name\n;

    $name = ;

    chomp($name);

    print Calling subroutine\n;

    &print_my_name($name);

    print Finally I am out of subroutine\n;

    sub print_my_name

    {

    print I am @_ and I am in subroutine now\n;

    }

  • 8/13/2019 Perl Functions and Subroutines

    11/14

    Returning Values

    Example:

    use strict;

    sub HowdyEveryone {

    return "Hello everyone.\nWhere do you want to go with Perl

    today?\n";}

    print &HowdyEveryone;

  • 8/13/2019 Perl Functions and Subroutines

    12/14

    Using Arguments

    Perl sets a special array variable, @_Declare a list of variables and assign @_

    Example:

    use strict;

    sub HowdyEveryone {

    my($greeting, @names) = @_;

    my $returnString;

    foreach my $name (@names) {

    $returnString .= "$greeting, $name!\n";

    }

    return $returnString ."Where do you want to go with Perl today?\n";

    }

    print &HowdyEveryone ("Hello!", "Prashanth", "Santosh", "Smitha", "Alia", "Anupama");

  • 8/13/2019 Perl Functions and Subroutines

    13/14

    Exercise-Subroutine

    Write a subroutine to get the sum,multiplication and division of 2 numbers.

    Call the subroutine with capturing the returnvalues in scalar variables and also returnvalues.

    After this is done, just modify the program toget inputs from the command line.

    (Hint: ARGV is the array which stores all the command line inputs)

  • 8/13/2019 Perl Functions and Subroutines

    14/14

    ImaginationAction Joy