Monday, May 26, 2008

Chapter 7: Subroutines

############################################################################################################
#Author: Leo
#Email: ahyeek@gmail.com
#Desc:
# The author has created some codes to test out during the learning process for the book called
# <Professional Perl Programming>, publisher: Wrox Pree Ltd, year in 2001
# ISBN: 1-861004-49-4.
#
#Summary:
# Examples code for: Subroutines
# Chapter 7: Subroutines
#
############################################################################################################


#!/usr/bin/perl

use warnings;
use strict;


#Testing global variable...
my $MY_GLOBAL_VARIABLE = "Global assigned variable!";

sub mysubroutine{
print "Hello Perl subroutine!! I am going to learn you!\n";
print "That will be easy like ABC :)...\n";

}

my $subref_print_empty_line = sub { print "\n"; };
my $subref_anonymous = sub { print "This is anonymous sub routine...\n"; };


#Call the sub routine that I created.
mysubroutine();

#Call again the sub routine...
&$subref_print_empty_line;
mysubroutine();

#Call anonymous sub routine.
&$subref_print_empty_line;
&$subref_anonymous;

######################################################
# Build reference to the sub routine.
######################################################

print "\nCalling reference sub routine...\n";
my $subref_mysubroutine = \&mysubroutine;
&$subref_mysubroutine;

######################################################
# Checking for subroutines defination
######################################################
&$subref_print_empty_line;

if(defined &mysubroutine)
{
print "mysubroutine is defined!\n";
}

if(not defined &mysubroutine_xx)
{
print "mysubroutine_xx is NOT defined!\n";

}

######################################################
# Passing parameters into subroutines
######################################################
sub volume_param_type_1{
(my $h, my $w, my $l) = @_;
return $h * $w * $l;

}

sub volume_param_type_2{
my $h = shift;
my $w = shift;
my $l = shift;
return $h * $w * $l;

}

sub print_msg_and_count{
my($message, @count) = @_;

#print scalar message information.
&$subref_print_empty_line;
print "Message --> " . $message . "\n";

foreach(@{count}){

print "Count element: $_\n";
}
}

sub print_arrmsg_and_count{

my($message, $count) = @_;

#print scalar message information.
&$subref_print_empty_line;
foreach(@ {$message}){

print "Message --> " . $_ . "...\n";
}

foreach(@ {$count}){

print "Count element: $_\n";
}
}

sub arraytext_return{

my ($value1,
$value2,
$value3) = @_;

return ("Processed value 1 = " . $value1,
"Processed value 2 = " . $value2,
"Processed value 3 = " . $value3);

}

&$subref_print_empty_line;
print "Call sub with param type 1 ==>" . volume_param_type_1(3, 3, 3); &$subref_print_empty_line;
print "Call sub with param type 2 ==>" . volume_param_type_2(3, 3, 3); &$subref_print_empty_line;


my $message = "msg 1";
my @message = ("msg 1", "msg 2", "msg 3", "firstname " . "lastname");

my @count = (1, 2, 3);
print_msg_and_count($message, @count);
print_arrmsg_and_count(\@message, \@count);


sub call_voltype1_voltype2{
print "Vol type 1 ==>" . &volume_param_type_1; &$subref_print_empty_line;
print "Vol type 2 ==>" . &volume_param_type_2; &$subref_print_empty_line;

}

&$subref_print_empty_line;
print "Inherit @_ from parents\n";
call_voltype1_voltype2 (1, 2, 3);


&$subref_print_empty_line;
my @myvalues = ("html value 1", "html value 2", "html value 3");
my @processesvalues = arraytext_return(@myvalues);
print_arrmsg_and_count(\@processesvalues, \@count);


######################################################
# Test global variable accessing...
######################################################

sub print_global_variable{
print "Accessing global variable inside sub routine =->" . $MY_GLOBAL_VARIABLE . "\n";

}

sub modify_global_variable{
print "Modifying global variable here ...\n";
$MY_GLOBAL_VARIABLE = "Modified global variable...";

}

&$subref_print_empty_line;
print_global_variable();
modify_global_variable();
print_global_variable();

######################################################
# Assign value sub...
######################################################
my $scalarsub = "Before assigning...";

sub assignablesub : lvalue {

$scalarsub;
}

&$subref_print_empty_line;
print $scalarsub , "\n";
assignablesub = "After assigning...";

print $scalarsub , "\n";

#######################################################################
# Passing parameter using hash variable into subroutine...
#######################################################################
print "\nPassing parameter using hash variable into subroutine...\n";
sub CheckPassParam {

my $arg = shift;

my $p1 = $arg->{'param1'};
my $p2 = $arg->{'param2'};
my $p3 = $arg->{'param3'};

print "\nParam1 = $p1, Param2 = $p2, Param3 = $p3\n";

foreach (sort keys %{$arg})

{
print "Key=$_, Param=$arg->{$_}\n";
}

}

CheckPassParam({
'param1' => 'Direct My first param',
'param2' => 'Direct My second param',
'param3' => 'Direct My third param'

});

No comments: