Portal:Perl CGI Programming

From Perl Guilds - Getting Medieval with Perl
Jump to navigation Jump to search

Most of the web-based perl work out there are long-standing small to medium size businesses whose stack is a combination of batch scripts/crons and CGIs.

The primary concerns are usually around adding modern features, or modernizing to PSGI, better templating engines and so forth.

In general you won't do well if you don't have a strong understanding of the major database engines, javascript, CSS and HTML.


Tips & Tricks:

  • CGI::Emulate::PSGI::emulate_environment - CGIs always rely on ENV, and PSGI doesn't set that for a variety of good reasons.
  • It is a common practice to feed raw requests into CGIs via stdin to rescue things like failed form submissions that get dumped somewhere. You'll have to build a shim.
  • The use of require() rather than use is very common in CGIs, as startup time is the enemy there. You'll have to hunt all that down to improve performance and stability.
  • Most PSGI servers are a preforking worker model. Reliance on global vars in packages set during BEGIN and other non-reentrant practices will almost certainly get you in trouble.
  • You'll almost certainly want to move away from CGI.pm HTML builder subs and into a templating engine. Text::XSlate using the TTerse dialect (template toolkit) is usually what you'll want to reach for.
  • Deploying code is a bit different than in CGI; you'll have to send SIGHUP to the PSGI server (most on cpan support this). That will instruct the server to gracefully reap the workers without dropping in-flight requests.

Specific Features:

  • To add 2FA, use TOTP. Recommended module is Trog::TOTP, it avoids edge cases present in all other options and performs better.
  • To add URL shortening without using external services (useful for things like Emails), use URI::Shortener.