MasterPage using HTML::Template

Can we create single master page which includes header,footer and left-right panel ?  Absolutely yes yes !!! Since morning I was working on it and finally Eureka !!!

Note: It can be achieved by filter parameter of  HTML::Template, we are going to see in below example

Let me start with step by step

Create Masterpage: /html/masterpage.tmpl

<div style=”background:red;width:100%;height:100px”>
Header Section
<div style=”background:white;width:100%;margin-top:200px” id=”content”>
<tmpl_include name=”childpage”>
<div style=”background:green;width:100%;height:100px;margin-top:200px”>
Footer Section

Create Page Module: /cgi-bin/lib/

package Page;
use CGI;
use HTML::Template;

my $query = CGI->new();
print $query->header();

sub output  {
my $hash = pop @_;

my $childpage = $hash->{‘childpage’};
my $filter = sub {
my $text_ref = shift;
$$text_ref =~ s/childpage/$childpage/

my $template = HTML::Template->new(filename => $hash->{‘masterpage’},filter => $filter);
print $template->output();

Create Child Pages: /html/ index.tmpl, /html/login.tmpl


This is the home page or index page of website


<tr><td>Username:</td><td><input type=’text’ /></td></tr>
<tr><td>Password:</td><td><input type=’text’ /></td></tr>
<tr><td colspan=”2″><input type=’submit’ /></td></tr>

Create CGI pages:  /cgi-bin/cgi/index.cgi,  /cgi-bin/cgi/login.cgi

1)  index.cgi

#!c:\perl\bin\perl.exe -w
use CGI::Carp qw(fatalsToBrowser);
use lib “./../lib/”;
use Page;

‘masterpage’ => ‘../../html/masterpage.tmpl’,
‘childpage’ => “index.tmpl”,

2)  login.cgi

#!c:\perl\bin\perl.exe -w
use CGI::Carp qw(fatalsToBrowser);
use lib “./../lib/”;
use Page;

‘masterpage’ => ‘../../html/masterpage.tmpl’,
‘childpage’ => “login.tmpl”,

Call CGI page:



Please contact me you want any query or want .rar file of this example.




what is subroutine?
Subroutines are the basic computer science methodology to divide tasks into subtasks.


sub version {
print “version of pl file.”;

It can be call by name without braces, but make sure subroutine defined before we use them



sub mysub {
print “mysub is called”;

# Following error comes if sub routine is called without braces

# Unquoted string “mysub” may clash with future reserved word at line 4.
# Useless use of a constant (mysub) in void context at line 4.

Parameters & Arguments

@_ is the list of incoming parameters to a sub.
$_ is known as the “default input and pattern matching space

sub mysub {
foreach(@_) {
print $_;


By default (implicitly) last statement of sub routine returns
Programmer must write return statement to return explicitly from sub routine.

Sub routine prototype 

Prototypes let you declare subroutines to take arguments just like many of the built-ins, that is, with certain constraints on the number and types of arguments. We call them “prototypes”, but they work more like automatic templates for the calling context than like what C or Java programmers would think of as prototypes. With these templates, Perl will automatically add implicit backslashes, or calls to scalar, or whatever else it takes to get things to show up in a way that matches the template


sub sum_of_two_squares($$)  {

my ($a, $b) = (shift, shift);

return $a**2 + $b**2;


sum_of_two_squares(1,2);      # No run-time error

sum_of_two_squares(1,2,3);  # ERROR


Declared as Called as
sub mylink ($$) mylink $old, $new
sub myreverse (@) myreverse $a,$b,$c
sub myjoin ($@) myjoin ":",$a,$b,$c
sub mypop (\@) mypop @array
sub mysplice (\@$$@) mysplice @array,@array,0,@pushme


How swap values of two variables

PERL is magical language, I have no doubt about it. See the following simple example to swap the value of two variables

#!/usr/bin/perl -w
use strict;
use warnings;

my $a = 11;
my $b = 22;
print “a = “, $a,”b = “,$b,”\n”;

($a,$b)= ($b,$a); # here is the magic
print “a = “, $a,”b = “,$b;

No temporary variable and complex logic as compare to other languages like c#, vb and java

It is piece of cake !!! Really



Distinguishing Class and Object Methods

First let me start with the definition of class and object method

What is Class Method? – Method which is called without creating an object of class.

What is Object Method? – Method which is called by the reference of object.

In PERL there is no such disquisition, but let me tell you the same can be done by adding just three lines of code in function

sub func1 {
my $self = shift;
unless( ref $self) {                                                                  # Line 1
croak “Should call func1() with an object, not a class”;         # Line 2
}                                                                                          # Line 3
return “1”;