[cgiapp] hook question

Cees Hek ceeshek at gmail.com
Thu Jul 29 19:48:34 EDT 2010


On Fri, Jul 30, 2010 at 7:58 AM, Todd Ross <tar.lists at yahoo.com> wrote:
> I understand the distinction between compile time and run time (as phases), but
> I'm not fully appreciating the impact here; I'm still a little lost.

Hi Todd,

You are actually running into a couple of problems, one of them is
what Michael has described, and the other is that the callback code
will not run the same callback method multiple times at the same hook
location.  You are hooking the same method three times, but it will
only ever be executed once.

So, place your instance code at the end of the file, and create a
separate callback for each call to add_callback and you will see that
it does work (ideally though, you should be placing your packages into
separate files as it will save you many headaches down the road --
especially when working with mod_perl or other persistant perl
processes):


#!/usr/bin/perl

use strict;
use warnings;

package Private::Webapp;

use base 'CGI::Application';

__PACKAGE__->add_callback('prerun', sub { print STDERR "hook_prerun 1\n" });
Private::Webapp->add_callback('prerun', sub { print STDERR "hook_prerun 2\n" });

sub setup {
       my $self = shift;

       $self->start_mode('begin');
       $self->mode_param('rm');
       $self->run_modes(
               begin => 'do_begin'
       );

       $self->add_callback('prerun', sub { print STDERR "hook_prerun 3\n" });
}

sub do_begin {
       my $self = shift;

       my $output = "Hello, CGI::Application!\n";
       return $output;
}

package main;

my $webapp = Private::Webapp->new();
$webapp->run();

1;


More information about the cgiapp mailing list