I wrote this up, and then saw CGI::Application with 'main' runmodes and 'sub' runmodes. Very related, but I still have some specific oddities, so I am going ahead and posting this anyway.

I am trying to organize my CGI::Application thusly because I want to create a common Account.pm that many different instances of web applications can draw upon. In fact, I want to package even some basic templates within Account.pm that can be automatically generated and used within more customized application-specific templates. So, when everything is working, I would have a common Account.pm somewhere in my person lib that I would be able to include in my web application instance and get all the account authentication code automagically.

web_root + - index.cgi + lib - App.pm - Account.pm - ... + templates - welcome.tmpl - login.tmpl - ... # in index.cgi ########################## use lib "lib"; use App; my $c = App->new( TMPL_PATH => "templates", PARAMS => { .. } ); $c->run(); ##################################### # in App.pm ########################### use base "CGI::Application"; use Account; sub setup { my $self = shift; $self->start_mode("view"); $self->mode_param("a"); $self->run_modes( "view" => "view", "edit" => "edit", .. "verify" => "Account::verify", "login" => "Account::login", "logout" => "Account::logout", "create account" => "Account::create", ); } sub view { my $self = shift; if ($self->session->param("~logged_in")) { # View stuff } else { $self->login; } } ##################################### # in Account.pm ######################## package Account; sub login { my ($self, %args) = @_; # login stuff $self->view; } sub logout { my ($self, %args) = @_; # logout stuff $self->view; } #####################################

With the above, of course, I get the error that there was an Error executing run mode 'view': Can't locate object method "login" via package "App" at.... If I change $self->login inside sub view {} to $self->Account::login then it works, but calling stuff from within Account.pm stops working.

This seems pretty basic to me, yet, embarrassingly for me, I can't figure out a solution. Is there a better way of doing this? Why doesn't $self->login work even though it is declared correctly inside setup?

--

when small people start casting long shadows, it is time to go to bed

In reply to Modularizing CGI::Application by punkish

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.