leocharre has asked for the wisdom of the Perl Monks concerning the following question:

I wanted to add some junk to a cgi app module (A.pm), so I made a module appart (B.pm), with more methods, and runmodes.. aha! And when I import the stuff into my main module.. I get

Invalid CODE attribute: Runmode

I want A.pm to import code in B.pm

A.pm is a CGI::Application app.

I am using CGI::Application::Plugin::AutoRunmode to define runmodes.

I get this error:

Invalid CODE attribute: Runmode

I was importing B.pm code into A.pm by "use base 'B.pm';", so I tried instead using 'Exporter' in B.pm and then 'use B;' in A.pm (also tried require)

I also tried using cgi app autorunmode in B.pm as well as in A.pm..

All give same error, any runmodes declared as "sub modename : Runmode {..}" which are not inside A.pm will stop compilation.

How do I get this to work... Please.. ? Any ideas?

update, here is an example..

In t/0.t:

use Test::Simple 'no_plan'; use lib './lib'; use A; my $a = new A; ok($a->run);

In lib/A.pm:

package A; use base 'CGI::Application'; use base 'B'; use CGI::Application::Plugin::AutoRunmode; use constant DEBUG => 0; use strict; sub setup { my $self = shift; $self->start_mode('importme'); $self->mode_param('rm'); } sub cgiapp_prerun { my $self = shift; print STDERR "I am prerun\n" if DEBUG; # This is needed to use a cgiapp_prerun witth AutoRunmode plugin CGI::Application::Plugin::AutoRunmode::cgiapp_prerun($self); } sub hithere : Runmode { my $self = shift; my $out = 'hi, there..'; return $out; } 1;

In B.pm:

package B; use CGI::Application::Plugin::AutoRunmode; sub importme : Runmode { my $self = shift; $self->header_type('redirect'); $self->header_props(-url=>'?rm=hithere'); return 'Redirecting..'; } 1;

I still get

xoot@moonshine temp# perl t/0.t
Invalid CODE attribute: Runmode at lib/B.pm line 4
BEGIN failed--compilation aborted at lib/B.pm line 9.
Compilation failed in require at (eval 4) line 3.
        ...propagated at /usr/lib/perl5/5.8.8/base.pm line 85.
BEGIN failed--compilation aborted at lib/A.pm line 4.
Compilation failed in require at t/0.t line 3.
BEGIN failed--compilation aborted at t/0.t line 3.
# Looks like your test died before it could output anything.
xoot@moonshine temp#

update I have realized now what was wrong. You must use base CGI::Application in the module you are importing!!

A.pm

use base 'CGI::Application'; use CGI::Application::Plugin::AutoRunmode; use base 'B';

B.pm

use base 'CGI::Application'; use CGI::Application::Plugin::AutoRunmode; sub useme : Runmode {}

Now, you can use runmodes in B.pm inside A.pm.

Replies are listed 'Best First'.
Re: CGI::Application::Plugin::AutoRunmode importing runmodes problem
by Joost (Canon) on May 18, 2007 at 22:35 UTC
      I posted some code, I apologize for missing that. I had tried indeed to use AutoRunmode in both modules.