[cgiapp] hook question

Todd Ross tar.lists at yahoo.com
Thu Jul 29 17:58:43 EDT 2010


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.

You indicate that method calls happen at runtime.  Assuming that these qualify 
as method calls ...

__PACKAGE__->add_callback('prerun', \&hook_prerun);
Private::Webapp->add_callback('prerun', \&hook_prerun);

... isn't the only time they can be processed during compile time?  They're not 
in a method that can be invoked at run time.

Even if the package were placed into a separate .pm file, you indicate that the 
use <package> statement is going to be processed at compile time, which seems 
like the same phase they're processed in in my existing example.  Why would the 
behavior change?

Todd



________________________________
From: Michael Peters <mpeters at plusthree.com>
To: CGI Application <cgiapp at lists.openlib.org>
Cc: Todd Ross <tar.lists at yahoo.com>
Sent: Thu, July 29, 2010 4:30:00 PM
Subject: Re: [cgiapp] hook question

On 07/29/2010 05:16 PM, Todd Ross wrote:

> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> my $webapp = Private::Webapp->new();
> $webapp->run();
> 
> package Private::Webapp;
> 
> use base 'CGI::Application';
> 
> # Neither of these work -- why?
> __PACKAGE__->add_callback('prerun', \&hook_prerun);
> Private::Webapp->add_callback('prerun', \&hook_prerun);

You're confusing runtime and compile time. Some things (like package and use 
statements) run at compile time. Other things (like any method calls) happen at 
run time. So your package is compiled and methods added, but those hooks won't 
be added until after $webapp->run() has already taken place.

But if you put the package into it's own file you wouldn't have this problem 
since the runtime code in the package is run when the "use Private::Webapp" 
statement is run.

-- Michael Peters
Plus Three, LP



      


More information about the cgiapp mailing list