Migrating CGI.pm-based code to Dancer2
Hello all, I'm attempting to merge an old CGI.pm-based code base into a Dancer2-powered app, and I have some questions. I had been running the old code using Plack::App::CGIBin to mount the .cgi files, but there is now enough code shared between the two that I am ready to merge the two. The shared code currently runs some preflight checks, and then parses the query params to figure out the correct modules to load and subroutine to dispatch. 1) The old code is written in a "print as you go" form. My initial idea was to capture the STDOUT, but unfortunately the code includes some hacks to keep the browser connection open when running long processes (rather than forking a child process and periodically checking on its process, using an event loop, etc.). There's lots of code like this: print "<p>Launching lengthy process...</p>"; my $data = do_something_that_takes_ages_here(); print "<p>Finished part one of lengthy process. Starting phase two!</p>"; I'd like to use Dancer2's streaming/async capabilities here but am not sure how to send what is printed to STDOUT to the 'content' keyword. Can anyone help? 2) I'm converting the PSGI env into a CGI object using CGI::PSGI; apart from HTTP headers, is there anything else I need to take care of? Thank you for any help and hints! Amelia.
On Aug 31, 2015, at 2:11 PM, Amelia Ireland <aireland@lbl.gov> wrote:
1) The old code is written in a "print as you go" form.
When I ran into the same situation in my code base, I handled it like this: my $html = “SOme stuff…”; $html .= “More stuff…”; …repeat until done :) … return $html; The tricky bit comes in translating things like this: print “Some “, $rather, “ complicated “, code(); You either end up replacing all the commas with dots or: $html .= sprintf “Some “, …etc.
2) I'm converting the PSGI env into a CGI object using CGI::PSGI; apart from HTTP headers, is there anything else I need to take care of?
I wouldn’t keep trying to hold onto CGI. Dancer’s always-on route-based processing method is quite different from CGI’s model. Trying to program Dancer in a CGI-like fashion might make some things a bit quicker to translate, but I think you’ll eat that savings up in areas that don’t map easily, and in difficulty of understanding the final version as it keeps jumping between the CGI and Dancer abstractions.
participants (2)
-
Amelia Ireland -
Warren Young