[cgiapp] hook question

Todd Ross tar.lists at yahoo.com
Fri Jul 30 11:39:29 EDT 2010


Hi Cees,

I'm not actually trying to add all three hooks at once; I'm only enabling one at 
a time.  The only one that was working was the $self->add_callback().

When I rearrange the code as you have, all three callbacks work, just as you 
say.  I don't understand why though.

What's the difference between:

#!/usr/bin/perl

use strict;
use warnings;

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

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" });

# ...

... and ...

#!/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" });


# ...

package main;

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

1;

It seems like adding package main; (which I believe is happening implicitly in 
the first example) and moving the code is changing the semantics of how it's 
processed even though things should still be happening in the same phases?

Todd



________________________________
From: Cees Hek <ceeshek at gmail.com>
To: CGI Application <cgiapp at lists.openlib.org>
Sent: Thu, July 29, 2010 6:48:34 PM
Subject: Re: [cgiapp] hook question

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;

#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://lists.openlib.org/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:  http://lists.openlib.org/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################


      


More information about the cgiapp mailing list