Embperl

mod_perl

What is the purpose of EmbperlObject?

EmbperlObject is intended for developing large module web sites. If number of pages (or large parts) of your sites contain the same information or code, that piece of information or code can be put into files or libraries and then executed / included into pages.

Common components such as navigation bar, site map, author (and other meta tags, except possibly for keywords and title) are prime candidate for EmbperlObject.

What is the purpose of the base.epl file?

The file specified by the EMBPERL_OBJECT_BASE directive in httpd.conf (usually named base.epl) is the lynchpin of how EmbperlObject operates. Whenever a request comes in for any page on this site, Embperl will look for base.epl first in the same directory as the request, and then up the directory hierarchy to the root of the web site. Embperl will first execute base.epl which in-turn execute the requested page to produce the output. Example base.epl:

<html>
    <head>
        <title>some title</tile>
    </head>
    <body>
        <p>[-Execute('*')-]</p>
    </body>
</html>

The '*' argument to Execute is a special filename. It literally means the file name that was actually request.

A modular web site:

// base.epl:
<html>
    [-Execute('constant.epl')-]
    [-Execute('init.epl')-]
    <head>
        <title>some title</title>
    </head>
    <body>
        [-Execute('*')-]
    </body>
    [-Execute('cleanup.epl')-]
</html>

// constant.epl:
[-
    $req = shift;
    $req->{bgcolor} = "white";
    $req->{webmaster} = "John Smith";
    $req->{website_database} = "mydatabase";
-]

// The req object is used to pass information between components.
// You can even use it to pass object references and other things as well.

// init.epl:
[-
    $req = shift;
    use DBI;
    use CGI;
    $dsn = "DBI:mysql:$req->{website_database}";
    $req->{dbh} = DBI->connect($dsn); // passing database connection
-]

// cleanup.epl:
[-
    $req = shift;
    $req->{dbh}->disconnect();
-]

As you can see, in a modular web site, the base.epl is broken up into several files. These files can be redefined in sub-directories to override the default. This kind of inheritance is referred to as inheritance of modular files via directory hierarchy. File /contact/init.epl can execute /init.epl to utilize code that has been defined, and override or add on portion specific to /contact/init.epl:

// /contact.epl:
[-Execute('../init.epl')-]
[-
    # Do some setup specific to this sub-directory
-]

What is the definition of 'inheritance of modular files via directory hierarchy'?

In a modular web site, the base.epl is broken up into several files. These files can be redefined in sub-directories to override the default. This kind of inheritance is referred to as inheritance of modular files via directory hierarchy. File /contact/init.epl can execute /init.epl to utilize code that has been defined, and override or add on portion specific to /contact/init.epl:

// /contact.epl:
[-Execute('../init.epl')-]
[-
    # Do some setup specific to this sub-directory
-]

What is the definition of 'inheritance of sub-routines'?

EmbperlObject also provide another inheritance referred to as inheritance of sub-routines. A sub-routine can be defined in two ways:

[!
    sub perl_sub {
        # some Perl code
    }
!]
[$ sub embperl_sub $]
    Some html
[$ end_sub $]
// /base.epl:
[! sub title("My site") !]
[- $req = shift -]
<html>
    <head>
        <title>[+ $req->title() +]</title>
    </head>
</html>

// /contact/index.html
[! sub title{"Contact"} !]
[- $req = shift -]
<html>
    Contact form goes here
</html>

As you can see, we defined the method title() in base.epl, so any page requested from the root directory will get the default title method. Then in /contact/index.html, we redefine the method title(). When a call to title() occurs in base.epl, the correct version of the method will be executed. This allows each file to potentially redefine methods that were defined in base.epl, but as your site get bigger, you will probably want to split off some routines into their own files.

How can we create files that contains only inheritable methods?

As you can see, we defined the method title() in base.epl, so any page requested from the root directory will get the default title method. Then in /contact/index.html, we redefine the method title(). When a call to title() occurs in base.epl, the correct version of the method will be executed. This allows each file to potentially redefine methods that were defined in base.epl, but as your site get bigger, you will probably want to split off some routines into their own files.

EmbperlObject also allows us to create files that contains only inheritable methods.

// /base.epl:
<html>
    [- $subs = Execute({object-> 'subs.epl'}); -]
    [-Execute('constant.epl')-]
    [-Execute('init.epl')-]
    <head>
        [-Execute('header.epl')-]
    </head>
    <body>
        [-Execute('*',$subs)-]
    </body>
    [-Execute('cleanup.epl')-]
</html>

// /subs.epl
[!
    sub hello {
        my ($self, $name) = @_;
        print OUT "Hello, $name";
    }
!]

// /insult/index.html:
[-
    $subs = $param[0];
    $subs->hello("Joe");
-]

// /insult/subs.epl:
[! Execute({isa => '../subs.epl'}) !]
[! 
    sub hello {
        my ($self, $name) = @_;
        $self->SUPER::hello($name);
        print OUT "you schmuck";
    }
!]
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License