Am I doing things correctly?

Not entirely, I suspect. You have 2 different CGI objects: 1 in test.cgi and 1 in Myapp. That seems inherently incorrect. Take the object you already have in test.cgi and pass it to the Myapp subs if that's what you want. Alternatively you could have Myapp subclass CGI::Fast and then just use that in test.cgi and call methods on that object from there. Or if you don't want to subclass CGI::Fast you could have a separate object of Myapp on which to call methods. TIMTOWTDI.

Here's a simplified example using that last of these approaches:

#!/usr/bin/perl -T package Myapp; use strict; use warnings; use Try::Tiny; sub new { my ($class, $q) = @_; return bless {q => $q}, $class; } sub appy { my $self = shift; my $action = $self->{q}->param('action') || 'bailout'; try { $self->$action; } catch { print "Unrecognized action : $action\n"; } } sub do_something { print "do something called\n"; } package main; use strict; use warnings; use CGI::Fast; use Try::Tiny; my $n = 0; while (my $q = CGI::Fast->new) { my $query = $q->param('node'); print "Content-type: text/plain\n\n"; ++$n; print "You are request number $n. Have a good day!\n"; print "query: $query\n"; my $app = Myapp->new ($q); try { $app->$query } catch { error() }; print "done!\n"; } sub error { print "An error occurred.\n"; }

Note that I've combined the module and script into one file for ease of this illustration (it's an SSCCE, albeit you need an FCGI-capable webserver to test it). If you want to split them out again, just add use Myapp; back into the script. I've added Try::Tiny to catch the missing methods rather than bother with dispatch tables just to show another way of doing this but there's nothing wrong with dispatch tables if you prefer them. Equally you could just use UNIVERSAL::can to test the method's existence - so many options! I've used strict and warnings everywhere and so should you.

To answer the other avenue of discussion, the while loop is absolutely necessary otherwise you will not have the persistence (ie. $n will never increase). Have a think for yourself about why this is.


🦛


In reply to Re: Getting query string with CGI::Fast by hippo
in thread Getting query string with CGI::Fast by Anonymous Monk

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.