Here's the script being tested:

#!/opt/perl524 use strict; use warnings; main() unless caller(); sub main { use CGI; my $cgi = CGI->new; if ( !$cgi->param ) { print $cgi->header, my_form( 'action_url' => $cgi->url() ); exit; } my $num1 = $cgi->param('num1') || ''; my $num2 = $cgi->param('num2') || ''; # Next, add the value of the incoming parameters and display a result +(not shown here) # my $sum = add_two_numbers( $num1, $num2 ); # Then print a result to the web browser and display a fresh form unde +r the result (not shown here) exit; } ## end sub main sub add_two_numbers { my ( $num1, $num2 ) = @_; return $num1 + $num2; } sub my_form { my %params = @_; my $action_url = $params{'action_url'} || ''; die "No URL specified for the 'action' attribute of the form, can' +t continue" if ( !$action_url ); my $form = qq|<html> <head> <title>Add Two Numbers</title> </head> <body> <h1>Add Two Numbers</h1> <form action="$action_url" method="post"> <p>Enter some first number: <input name="num1"></p> <p>Enter some second number: <input name="num2"></p> <p><input type="submit" value="Add Now"></p> </form> |; } ## end sub my_form

Here's the test script:

#!/opt/perl524 use strict; use warnings; use Test::More ('no_plan'); my $SCRIPT_LOCAL_LOCATION = 'C:\Users\davel\Documents\scripts\add_two_numbers_modified_as_ma +in.cgi'; require_ok($SCRIPT_LOCAL_LOCATION) or exit; ok( add_two_numbers( 3, 17 ) == 20, "3 plus 17 produces 20" ); done_testing();

Here are the results of running the test script:

ok 1 - require 'C:\Users\davel\Documents\scripts\add_two_numbers_modif +ied_as_main.cgi'; ok 2 - 3 plus 17 produces 20 1..2

But when I change the first line of the script being tested, so as to be the following:

#!/opt/perl524 use strict; use warnings; main() if ( !caller() ); sub main { use CGI; my $cgi = CGI->new; if ( !$cgi->param ) { print $cgi->header, my_form( 'action_url' => $cgi->url() ); exit; } my $num1 = $cgi->param('num1') || ''; my $num2 = $cgi->param('num2') || ''; # Next, add the value of the incoming parameters and display a result +(not shown here) # my $sum = add_two_numbers( $num1, $num2 ); # Then print a result to the web browser and display a fresh form unde +r the result (not shown here) exit; } ## end sub main sub add_two_numbers { my ( $num1, $num2 ) = @_; return $num1 + $num2; } sub my_form { my %params = @_; my $action_url = $params{'action_url'} || ''; die "No URL specified for the 'action' attribute of the form, can' +t continue" if ( !$action_url ); my $form = qq|<html> <head> <title>Add Two Numbers</title> </head> <body> <h1>Add Two Numbers</h1> <form action="$action_url" method="post"> <p>Enter some first number: <input name="num1"></p> <p>Enter some second number: <input name="num2"></p> <p><input type="submit" value="Add Now"></p> </form> |; } ## end sub my_form

Then when I run the identical test script, I get:

not ok 1 - require 'C:\Users\davel\Documents\scripts\add_two_numbers_m +odified_as_main.cgi'; # Failed test 'require 'C:\Users\davel\Documents\scripts\add_two_num +bers_modified_as_main.cgi';' # at C:\Users\davel\Documents\scripts\add_two_numbers_modified_as_ma +in.cgi.t line 11. # Tried to require ''C:\Users\davel\Documents\scripts\add_two_numb +ers_modified_as_main.cgi''. # Error: C:\Users\davel\Documents\scripts\add_two_numbers_modifie +d_as_main.cgi did not return a true value at (eval 8) line 2. 1..1 # Looks like you failed 1 test of 1.

In reply to Re^2: Testing legacy CGI scripts using the "main() unless caller()" technique by davebaker
in thread Testing legacy CGI scripts using the "main() unless caller()" technique by davebaker

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.