[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cgiapp] Fatal error after query() becomes undefined?


Hello mailing list,

I am new to both mailing list and OOP. With a little help from Cees Hek
I have managed to get a working web application where I can login,
display a default page and (almost) logout again (see below). I'm using
Template Toolkit and CGI::FormBuilder to manage my html.

But now I have run into my first OOP problem. Clicking a 'logout' link
on the web-page invokes the logout() run-mode, where the current session
is expired (I'm using C::A::Plugin::Session to handle sessions), and
then we go to the login() sub which loads the login page template. This
sub is shared by the default run-mode if the user calls the script
without an active session. But for some reason this causes a 500 error:

Error executing run mode 'logout': Can't call method "query" on an
undefined value at MyApp.pm line 36.

Line 36: my $form = CGI::FormBuilder->new( # a query() param is expected
here - see below

No such error is generated if we invoke the login sub via
$self->prerun_mode('login') called as a result of
$session->param('~logged-in') evaluating to false.

These two subs are below:

sub login { # called if user not logged in, or has just logged out:
  my $self = shift;

  my $form = CGI::FormBuilder->new(
    fields => [ qw/UserID pwd/ ],
    validate => { UserID => 'NAME', pwd => 'VALUE' },
    labels => { pwd => 'Password' },
    name => 'login',
    template => {
      type => 'TT2',
      template => 'MyApp/login.tmpl',
      variable => 'login',
      data => {
        submitted_login => $self->query->param('_submitted_login') || ''
      },
      engine => \%tt_config, # hash defined elsewhere for reusability
    }
  );

  return $form->render();
}

sub logout { # called via link from web-page:
  my $self = shift;

  $self->session->delete(); # delete session
  $self->tt_params( username => undef ); # delete UserID param;
        # tt_params_clear doesn't work!!
  login(); # causes fatal error with query() undefined
}

The problem seems to be that by the time we get to login(), $self->query
has become undefined. The offending line is the submitted_login =>
$self->query->param('_submitted_login'), which if commented out allows
the script to run unimpeded. This line acts as a flag to the login
template to alert the user that their submitted UserID and/or password
values were unrecognised.

I cannot see much difference between Dumper($self->query() ) captured at
first login, and again after logout. So what's happening here - why does
query() appear to become undefined during progression to the login()
sub? Of course it won't allow a $self->query() ||= '' to 'define' it.

Incidentally, as commented above, $self->tt_params_clear generates a
server error, as this function doesn't appear to be recognised:

Error executing run mode 'logout': Can't locate object method
"tt_params_clear" via package "MyApp" at MyApp.pm line 155.
-- 
Richard Jones
Leeds, UK
mailto:suppressed

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.