.:: Welcome To My Personal Blog ::.

Saturday, March 5, 2011

Beginner's Guide To Travel With Perl - Part 8

Function

So, what will be the next? It’s function. In Perl, functions have another name “Subroutines”. It’s almost easy like other programming languages. Let’s see some examples:

Example1:


use strict;
sub pr { #description of subroutine
print "inside the subroutine\n";
}
print "subroutine example\n";
print "calling it\n";
≺ #calling subroutine, only “pr” also works here
print "The End\n";



Example2:

use strict;
sub sqr {
my $num = shift; #shift used to receive the sent element
print "inside the subroutine, square is " . $num * $num . "\n";
return $num * $num;
}
print "subroutine example\n";
print "calling it\n";
my $sq = sqr(5); #calling subroutine and sending parameter
print "square of 5 is $sq\n";

Example3:

use strict;
sub str {
my @r = @_; #receiving the sent array

print "elements $r[0]\n";
print "elements $r[1]\n";
print "elements $r[2]\n";
}
print "sending and receiving array\n";
my @st = ("a", "b", "c");
str(@st); #calling subroutine with array as parameter
print "The End\n";



Help Links:

http://www.perl.com/pub/2000/10/begperl1.html
http://perldoc.perl.org/perlintro.html

http://www.perl.org/learn.html
http://learn.perl.org/books.html
http://learn.perl.org/tutorials/
http://www.perl.com/pub/2008/04/23/a-beginners-introduction-to-perl-510.html
http://docstore.mik.ua/orelly/perl2/prog/ch01_05.htm
http://www.tizag.com/perlT/perluserinput.php
http://www.tizag.com/perlT/perlchomp.php

http://perldoc.perl.org/functions/use.html
http://www.well.ox.ac.uk/~johnb/comp/perl/intro.html

http://www.sthomas.net/roberts-perl-tutorial.htm/ch22/use_strict_
http://docstore.mik.ua/orelly/perl/prog3/ch31_19.htm
http://debugger.perl.org/580/perldebtut.html
http://perldoc.perl.org/strict.html




. . . . . The End . . . . .

No comments:

Post a Comment

Popular Posts