# NAME CGI::Alternatives - Documentation for alternative solutions to CGI.pm
# VERSION 0.19 # DESCRIPTION This module doesn't do anything, it exists solely to document alternatives to the [CGI](https://metacpan.org/pod/CGI).pm module. # BUT WHY? CGI.pm hasn't been considered good practice for many years, and there have been alternatives available for web development in perl for a long time. Despite this there are still some perl developers that will recommend the use of CGI.pm for web development and prototyping. The two main arguments for the use of CGI.pm, often given by those developers, are no longer true: 1) "CGI.pm is a core module so you don't have install anything extra." This is now incorrect as perl 5.22 has been released and no longer ships with it: http://perl5.git.perl.org/perl.git/commitdiff/e9fa5a80 If you are doing any serious web development you are going to have to use external dependencies, DBI is not in the core for example. 2) "CGI.pm scripts are shorter and simpler than alternative implementations." Again, not true and the following examples will show that. # NOTE ABOUT THE EXAMPLES All of the following are functionally identical. They display a very simple form with one text input box. When the form is submit it is redisplayed with the original input displayed below the input box. This example may be trivial, but that is the point. The frameworks shown here feature a great deal of functionality for dealing with other parts of your application and dealing with that in a maintainable way, with full separation of concerns and easy testing. All the examples are commented, where i feel it is necessary to highlight the differences between the implementations, however i do not explain the details of the frameworks - i would be duplicating the framework's docs if i did that, so have a look at the links provided and investigate further. All of the examples in this documentation can be found within the examples/ directory within this distribution. If you want to run them you will need to install the necessary CPAN modules, these are not included as dependencies in this distribution. # RAW CGI.pm EXAMPLES This is the base script that will be re-implemented using the other frameworks There are two versions - one that uses the HTML generation functions of CGI.pm and one that uses Template Toolkit. This is where we get into the first issue with CGI.pm - poor separation of concerns. CGI.pm (and cgi-lib.pl) existed years before template engines were available in perl. As a consequence, to make the generation of html easier, functions were added to output HTML direct from scripts themselves. In doing this you immediately increase the maintenance burden as any changes required to the HTML need to be done within the scripts. You can't just hand a template to the web-designers and allow them to work their magic. Don't mix the business logic and the presentation layer. Just don't. ## CGI.pm With Inline HTML Functions A simple example with form using the html generation functions of CGI.pm. Please don't use these functions, i am merely showing them here for comparison reasons. #!/usr/bin/env perl # most CGI.pm scripts i encounter don't use strict or warnings. # please don't omit these, you are asking for a world of pain # somewhere down the line if you choose to develop sans strict use strict; use warnings; use CGI qw/ -utf8 /; my $cgi = CGI->new; my $res = $cgi->param( 'user_input' ); my $out = $cgi->header( -type => 'text/html', -charset => 'utf-8', ); # html output functions. at best this is a lesson in obfuscation # at worst it is an unmaintainable nightmare (and i'm using # relatively clean perl code and a very very simple example here) $out .= $cgi->start_html( "An Example Form" ); $out .= $cgi->start_form( -method => "post", -action => "/example_form", ); $out .= $cgi->p( "Say something: ", $cgi->textfield( -name => 'user_input' ), $cgi->br, ( $res ? ( $cgi->br, "You wrote: $res" ) : () ), $cgi->br, $cgi->br, $cgi->submit, ); $out .= $cgi->end_form; $out .= $cgi->end_html; print $out; If you really want to continue using the HTML generation functionality of CGI.pm then you should take a look at [HTML::Tiny](https://metacpan.org/pod/HTML%3A%3ATiny) instead, which may give you a migration path away from CGI.pm's html generation functions; i strongly encourage you to move towards template driven page generation for anything involving markup as it will make porting your app to other frameworks much easier in the long run. ## CGI.pm Using Template Toolkit I'm including this example to show that it is easy to move the html generation out of the raw CGI.pm script and into a template for better separation of concerns. #!/usr/bin/env perl # most CGI.pm scripts i encounter don't use strict or warnings. # please don't omit these, you are asking for a world of pain # somewhere down the line if you choose to develop sans strict use strict; use warnings; use FindBin qw/ $Script $Bin /; use Template; use CGI qw/ -utf8 /; # necessary objects my $cgi = CGI->new; my $tt = Template->new({ INCLUDE_PATH => "$Bin/templates", }); # the user input my $res = $cgi->param( 'user_input' ); # we're using TT but we *still* need to print the Content-Type header # we can't put that in the template because we need it to be reusable # by the various other frameworks my $out = $cgi->header( -type => 'text/html', -charset => 'utf-8', ); # TT will append the output to the passed referenced SCALAR $tt->process( "example_form.html.tt", { result => $res, }, \$out, ) or die $tt->error; print $out; ## The Template File Here's a key point - this template file will be re-used by **all** the following framework examples with absolutely no modifications. We can move between the frameworks without having to do any porting of the HTML because it has been divorced from the controller code. What did i say? Separation of concerns: win. An Example Form