in reply to Getting query string with CGI::Fast
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.
🦛
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Getting query string with CGI::Fast
by Anonymous Monk on Jul 28, 2020 at 09:51 UTC | |
by hippo (Archbishop) on Jul 28, 2020 at 10:21 UTC | |
by Anonymous Monk on Jul 28, 2020 at 12:08 UTC | |
by hippo (Archbishop) on Jul 28, 2020 at 12:36 UTC | |
by Anonymous Monk on Jul 28, 2020 at 14:54 UTC | |
|