Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Testing legacy CGI scripts using the "main() unless caller()" technique

by etj (Deacon)
on Jul 20, 2022 at 20:15 UTC ( [id://11145608]=note: print w/replies, xml ) Need Help??


in reply to Testing legacy CGI scripts using the "main() unless caller()" technique

I couldn't see any indication above that you've written some small test scripts with various combinations of the components mentioned here, done a require on each of them, and seen what the results of each were?
  • Comment on Re: Testing legacy CGI scripts using the "main() unless caller()" technique
  • Download Code

Replies are listed 'Best First'.
Re^2: Testing legacy CGI scripts using the "main() unless caller()" technique
by davebaker (Pilgrim) on Jul 20, 2022 at 20:55 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11145608]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 14:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found