If the 'login' page is also part of your CGI::Application module, then you can use the 'prerun_mode' method to change the current runmode to another. That is how I handle authentication is some of my apps.

unless ( defined $user ) { $self->prerun_mode('login'); return; }

If your login script is not part of the CGI::App, then you can do the redirect using CGI::Apps method for redirection, and then change the runmode to a null runmode.

# If the user is not logged in, redirect to the login page unless ( defined $user ) { $self->prerun_mode('null_mode'); $self->header_props({-location => '/login.pl'}); $self->header_type('redirect'); return; } # A runmode that doesn't do anything sub null_mode { }

One of the most important things to remember about CGI::Application is that you never ever use the print command to send output to the browser. If you ever catch yourself using print, then step back and figure out why you really need to use it. Chances are there is a better way to do it.


In reply to Re: Strategies for exiting early for CGI::Application based webapp. by cees
in thread Strategies for exiting early for CGI::Application based webapp. by EvdB

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.