#!/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.
|