Re: [cgiapp] hook question

2010-07-30 Thread Todd Ross
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 cees...@gmail.com
To: CGI Application cgiapp@lists.erlbaum.net
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.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/ ##
####



  

#  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

2010-07-30 Thread Michael Peters
On 07/29/2010 05:58 PM, Todd Ross 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.

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

Yes those are method calls and they happen at run time. So they happen 
after you've already created and run your application.

 ... 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.

No, they aren't any different than the methods you invoked to create 
your application object (new) nor the method you invoked to run it (run).

 Even if the package were placed into a separate .pm file, you indicate that 
 the
 usepackage  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?

use will find the package, compile it and then run it, returning the 
value of the last operation in that package (which is why you always see 
Perl packages with a 1; at the bottom, so that it has a successful 
statement to run that returns a true value).

In your case the same basic things happen except that there is no use 
to compile and run the package before you create and run your 
application object. So perl compiles that script (including your web app 
package) and then starts executing the script. And you create and run 
your application object before you run those callbacks, so you never see 
them executing.

-- 
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/ ##
####




[cgiapp] hook question

2010-07-29 Thread Todd Ross
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

2010-07-29 Thread Michael Peters
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

2010-07-29 Thread Todd Ross
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

2010-07-29 Thread Cees Hek
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/ ##
####