Junco Code - DisplayPageOO.pm - Inside-Out Object Version #juncocode #objectoriented code=yes #!/usr/bin/perl -wT use strict; $|++; use lib '/Kestrel/lib'; use Kestrel::DisplayPageOO; print "hello\n"; my %hash; $hash{function} = "riding the horse"; DisplayPageOO->do_invalid_function(\%hash); DisplayPageOO->report_error("user", "testing", "debug"); my $a = DisplayPageOO->new("test"); $a->set_template_variable("hello", "first message"); $a->set_template_variable("world", "second message"); $a->display_page(); ################################### package DisplayPageOO; use strict; use warnings; use NEXT; use Hash::Util::FieldHash 'fieldhash'; { $CGI::HEADERS_ONCE=1; use HTML::Template; use Config::Config; use Kestrel::Modules; use Kestrel::User; fieldhash my %template_of ; sub new { my ($class, $template_name) = @_; my $self = bless \do{my $anon_scalar}, $class; my $kestrel_th = Config::get_value_for("template_home"); $ENV{HTML_TEMPLATE_ROOT} = $kestrel_th; $template_of{$self} = HTML::Template->new(filename => "$kestrel_th/$template_name.tmpl"); return $self; } sub set_template_variable { my ($self, $var_name, $var_value) = @_; $template_of{$self}->param("$var_name" => $var_value); } sub set_template_loop_data { my ($self, $loop_name, $loop_data) = @_; $template_of{$self}->param("$loop_name" => $loop_data); } sub display_page { my ($self, $function) = @_; my @http_header = ("Content-type: text/html;\n\n", ""); my $http_header_var = 0; print $http_header[$http_header_var]; #$template_of{$self}->param("home_page" => Config::get_value_for("home_page")); __set_template_variable($self, "home_page", Config::get_value_for("home_page")); __set_template_variable($self, "loggedin", User::get_logged_in_flag()); __set_template_variable($self, "username", User::get_logged_in_username()); __set_template_variable($self, "cgi_app", Config::get_value_for("cgi_app")); __set_template_variable($self, "site_name", Config::get_value_for("site_name")); print $template_of{$self}->output; exit; } sub do_invalid_function { my ($self, $tmp_hash) = @_; my $function = $tmp_hash->{function}; $self->report_error("user", "Invalid function: $function", "It's not supported."); } sub report_error { my ($self, $type, $cusmsg, $sysmsg) = @_; my $o = $self->new("$type" . "error"); $o->set_template_variable("cusmsg", "$cusmsg"); if ( $type eq "user" ) { $o->set_template_variable("sysmsg", "$sysmsg"); } elsif ( ($type eq "system") and Config::get_value_for("debug_mode") ) { $o->set_template_variable("sysmsg", "$sysmsg"); } $o->set_template_variable("referer", Utils::get_http_referer()); $o->display_page("Error"); exit; } sub DESTROY { my ($self) = @_; $self->EVERY::__destroy; } ##### private routines sub __destroy { my ($self) = @_; delete $template_of{$self}; } sub __set_template_variable { my ($self, $var_name, $var_value) = @_; $template_of{$self}->param($var_name, $var_value); } } 1;