You are on page 1of 14


Built-in Functions and Subroutines

Built-in Functions Built-in Functions by Category

Defining Subroutines Returning Values Using Arguments Passing Arrays and Hashes by Reference Handling return Values

Built-in Functions
Terms in an expression along with literals and variables
Can be used either with or without parentheses around their arguments

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 line

For arrays, hashes and strings

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

Example:$name = Prashanth; for($i=1;$i<=2;$i++) { chop($name); } print $name;

length(); #Gets length of string

split ();
Example:while (<FILE>) { 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)

Built-in Functions Partial List

Functions for SCALARs or strings
chomp, 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 functions abs, 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

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

Use the chop() function in a program. Print both the returned character and the string that 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.


User written functions Are called like functions Makes code clean Code reuse Called in program as
&sub_name Old method

sub_name(); New method

Defining Subroutines
Use the keyword sub
followed by name followed by a code block Example
print 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; }

Returning Values
use strict;

sub HowdyEveryone { return "Hello everyone.\nWhere do you want to go with Perl today?\n";
} print &HowdyEveryone;

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");

Write a subroutine to get the sum, multiplication and division of 2 numbers. Call the subroutine with capturing the return values in scalar variables and also return values. After this is done, just modify the program to get inputs from the command line.

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