Separation of Application Logic and Content |
After having separated the content from the layout, we usually still
have content mixed with application logic. To isolate the application
logic from the content, Embperl::Object provides with
EMBPERL_OBJECT_APP the possibility to define a file,
which contains all application code. For the Embperl website, the
application code resides epwebapp.pl. For loading it,
Embperl::Object searches the same path as for all other
included elements and the base template. For each application file loaded this way, Embperl create on the fly a package and
a hash reference. It then blesses the hash reference into the
package. So it provides easy object-oriented access to the
application. (Because Embperl already does this, you should not include
a package statement in that file.)
The application code file also will be automatically
inherited from Embperl::App via @ISA . This
enables easy access to all methods of superior objects as e.g session
handling.
Also note that the application file only contains Perl code and no markup,
since we are defining the application logic. After loading the application code and preparing all request related
information (like e.g. submitted form data, session data),
Embperl::Object calls the method
init , which - as usual for Perl methods - get's a reference
to the application object as first parameter. The second parameter is
Embperl's request object. The following init method is used at the emperl website
e.g. to generate the menus. sub init
{
my ($self, $r) = @_;
my $config = Execute({ object => 'config.pl', syntax => 'Perl' });
$config->new($r) ;
$r->{config} = $config ;
$r->{menu} = $config->get_menu($r);
fill_menu($config, $r->{menu}, $r->{baseuri}, $r->{root});
my $filename = map_file($r);
$r->param->filename($filename);
return 0;
} First the file config.pl is loaded and used to generate an
object (as happend with the application code file itself), which is
returned by Execute() . Then it initializes the new object
by calling its new method and generates a menu by calling
the method get_menu .
|