PERL/C2/Functions-in-Perl/English
Title Of Script: Functions in Perl
Author: Amol Brahmankar
Keywords: Functions in Perl, video tutorial, sub, subroutine, function without arguments or return value, function with arguments, function with a single return value, function with a multiple return values
|
|
Slide | Welcome to the spoken tutorial on Functions in Perl. |
Slide: Learning Objectives | In this tutorial, we will learn about -
|
Slide: System Requirements | For this tutorial, I am using
You can use any text editor of your choice. |
Slide: Prerequisites | You should have basic knowledge of variables, comments, loops, conditional statements and Data Structures in Perl.
|
We will first see some simple Perl functions. | |
Slide | sub simple_func {
# piece of code } |
Slide | * In Perl, functions, also called as subroutines, are declared with sub keyword.
|
Slide | Note:
use ModuleFileName; |
Let us understand this using asample program. | |
Directly switch to the file in gedit | Open a file in your text editor and name it as simpleFunction dot pl
|
In gedit
printText();
print “This is illustration of simple Perl Function\n”; } |
Type the code as displayed on the screen.
<<pause>>
Then, the execution control is passed to that function.
|
Save your file. | |
Switch to terminal
|
Then switch to the terminal and execute the Perl script by typing
|
Highlight the output on the terminal
|
The output will be as shown <<pause>>
|
Now, let us see a function with arguments. | |
Slide | sub func_with_parameters {
($variable1, $variable2,...$variableN) = @_; # Action to be perform on the arguments pass to this function } |
Let us understand this function using a sample program. | |
Directly switch to the file in gedit | Open a file in your text editor and name it as functionWithArgs dot pl
|
Gedit
addVariables(10, 20);
($var1, $var2) = @_; $addition = $var1 + $var2; print “Addition is: $addition\n”; } |
Type the following piece of code as shown on the screen.
<<pause>>
|
Save your file. | |
Slide | Note:
$var1 = shift @_; $var2 = shift @_;
|
Terminal
|
Now, switch to terminal and execute the script
by typing -
and press Enter
<pause>> |
Now, let us look at a functions which returns a single value. | |
Slide | sub return_single_value {
#piece of code
} |
Let us understand the same using a sample program. | |
Directly switch to the file in gedit | Let me switch to funcWithSingleRtrnVal dot pl script in gedit. |
Gedit
$addition = addVariables(10, 20); print “Addition is: $addition\n”;
($var1, $var2) = @_; $add = $var1 + $var2; return $add; } |
Open a file in your text editor and type the following piece of code as shown.
|
Save the file. | |
Terminal
perl funcWithSingleRtrnVal.pl
|
Now let us execute the script. So, switch to terminal and type-
and press Enter
<pause>> |
Now, let us see a function which returns multiple values. | |
Slide | sub return_multiple_values {
#piece of code
} |
Let us understand the same, using a sample program. | |
Directly switch to the file in gedit | In gedit, I have opened a file and named it as funcWithMultipleRtrnVals dot pl
|
Gedit
use Data::Dumper;
($var1, $var2, $addition) = addVariables(10, 20); print "Addition of $var1 and $var2 is: $addition\n";
($var1, $var2) = @_; $add = $var1 + $var2; return ($var1, $var2, $add); }
print "Array\n"; print Dumper \@marksArray;
print "Hash\n"; print Dumper \%marksHash;
@mArray = (98, 96, 95); return @mArray; }
%mHash = ( 'English' => 98, 'Physics' => 96, 'Chemistry' => 95 ); return %mHash; } |
Now, type the following piece of code as shown.
|
Press ctrl + s | Save your file. |
Terminal
Array $VAR1 = [ 98, 96, 95 ]; Hash $VAR1 = { 'Chemistry' => 95, 'English' => 98, 'Physics' => 96 }; |
Now let us execute the Perl script on the terminal by typing -
and press Enter.
<pause>>
|
Slide | Perl provides several inbuilt functions.
We learnt some of them in earlier tutorials. For eg- Arrays, Hash, sort, scalar, each, keys etc..
|
Slide: Summary | Let us summarize.
In this tutorial, we have learnt -
using sample programs. |
Slide: Assignment | Here is assignment for you -
|
About the Project | Watch the video available at the following link
download and watch it |
Spoken Tutorial Workshops | The Spoken Tutorial Project Team
test
contact at spoken hyphen tutorial dot org |
Acknowledgment | Spoken Tutorial Project is a part of the Talk to a
Teacher project
Education through ICT, MHRD, Government of India.
spoken hypen tutorial dot org slash NMEICT hyphen Intro |
Hope you enjoyed this Perl tutorial.
This is Amol Brahmankar signing off.
|