[cgiapp] hook question
Hello, I'm playing around with hooks in CGI::Application and am running into behavior that I don't understand. Given this script (suitable to run from the command line): [...@unixbox1:~] cat testcgiapp.pl #!/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); sub setup { my $self = shift; $self-start_mode('begin'); $self-mode_param('rm'); $self-run_modes( begin = 'do_begin' ); # this works $self-add_callback('prerun', \hook_prerun); } sub do_begin { my $self = shift; my $output = Hello, CGI::Application!\n; return $output; } sub hook_prerun { my $self = shift; print STDERR hook_prerun\n; } 1; [...@unixbox1:~] perl testcgiapp.pl hook_prerun Content-Type: text/html; charset=ISO-8859-1 Hello, CGI::Application! Why don't my package/class level hooks work but my instance/self hook does? I'm not actually trying to register all three hooks; just one at a time. I included all three to demonstrate what I've tried. Todd # CGI::Application community mailing list #### ## To unsubscribe, or change your message delivery options, ## ## visit: http://www.erlbaum.net/mailman/listinfo/cgiapp## #### ## Web archive: http://www.erlbaum.net/pipermail/cgiapp/ ## ## Wiki: http://cgiapp.erlbaum.net/ ## ####
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 # CGI::Application community mailing list #### ## To unsubscribe, or change your message delivery options, ## ## visit: http://www.erlbaum.net/mailman/listinfo/cgiapp## #### ## Web archive: http://www.erlbaum.net/pipermail/cgiapp/ ## ## Wiki: http://cgiapp.erlbaum.net/ ## ####
Re: [cgiapp] hook question
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 mpet...@plusthree.com To: CGI Application cgiapp@lists.erlbaum.net Cc: Todd Ross tar.li...@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 # CGI::Application community mailing list #### ## To unsubscribe, or change your message delivery options, ## ## visit: http://www.erlbaum.net/mailman/listinfo/cgiapp## #### ## Web archive: http://www.erlbaum.net/pipermail/cgiapp/ ## ## Wiki: http://cgiapp.erlbaum.net/ ## ####
Re: [cgiapp] hook question
On Fri, Jul 30, 2010 at 7:58 AM, Todd Ross tar.li...@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://www.erlbaum.net/mailman/listinfo/cgiapp## #### ## Web archive: http://www.erlbaum.net/pipermail/cgiapp/ ## ## Wiki: http://cgiapp.erlbaum.net/ ## ####