What should I do after printing HTML in my Perl CGI script?

What should you name after printing HTML from a Perl CGI script? I saw empty return , exit return , and in some cases nothing at all. Does it matter?

 #!perl print "Content-type: text/html\n\n"; print <<'END_HTML'; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Hello world!</title> </head> <body> <h1>Hello world!</h1> </body> </html> END_HTML # do anything else here? # return; # exit; 

Update

Suppose you have some tests in which you print HTML that is not at the very end of the file. In this case, it is more clear to call exit or return to visually show that the script should complete at this time? I know this is not the best way to write this - please just take it at face value for the sake of the question.

 #!perl use CGI; my $q = CGI->new(); my $action = $q->param('action'); my $html_start = "Content-type: text/html\n\n"; $html_start .= <<'END_HTML'; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Hello world!</title> </head> <body> END_HTML my $html_end = <<'END_HTML'; </body> </html> END_HTML if ($action eq 'foo') { print $html_start; print '<p>foo</p>'; print $html_end; # do anything else here? } else { print $html_start; print '<p>bar</p>'; print $html_end; # do anything else here? } 
+4
source share
6 answers

You should always print "\ n \ n" at the end of the output to tell the web server that the output is complete. Apache can do this for you upon successful completion of the script.

If your script is finished, you should technically call "exit" with the appropriate exit status - zero for success, other than zero for errors. The web server checks the exit status and displays the corresponding page (error code 500 is typical if the exit status shows anything other than success). If you do not specify a direct value for return or exit exit, Perl (in this case) will succeed, and the web server will process your CGI script as if you had called exit (0).

All this is described in the CGI specification (RFC 3875 - current, IIRC).

+4
source

I think this is more a question about coding style than functionality. If you have a simple and simple program without branching, there is no reason to mark the end of the program as something special:

  • return will not return anything that any user can use, since your program ends here.
  • exit will just exit with status 0, which naturally will happen anyway

However, if the structure is more complex, I would advise marking the end point with something specific, to tell someone who is reading your code that there is nothing more to consider for this particular set of conditions.

I would use exit rather than return, for the simple reason that the return may be mistaken at the end of the sub, while the output very specifically indicates that the program ends at this point.

+3
source

No, that doesn't matter. I always let the script die by itself, and not "return"; or "exit"; since there is no need for any of these statements (at the end of the perl document), and this is the same as exit; or 'exit 0;'.

+1
source

I would recommend return instead of exit, so that if you switch to CGI :: Fast, you can just turn it into sub.

+1
source

it makes sense that you don’t have to come back or go out if it's a separate thing. but what if this fragment is inside a function? will it still apply? for some reason came back or went out? Is it still just a style?

0
source

To answer your “updated” question: in my current application, I use general methods to do something like this:

 # UI.pm package UI; sub top { print "<html><head><title>$_[0]</title><body>"; } sub bottom { print "</body></html"; } # All the individual .cgi files package main; UI::top('Welcome to my page'); print "<p>Welcome to my page!</p>"; UI::bottom(); 

If you feel adventurous, you can “hook” BEGIN and END to do this automatically, but I wouldn’t - I think it would be too much to mess around for some pretty little benefit.

This is not how I will do it now if I had to do it all: I would rely more on some middle level, such as Catalyst, which would allow me to quickly switch to mod_perl or back to CGI. Or even better, write my app from scratch to use more AJAX. Thus, most of my "actions" are performed by really small Perl scripts that process only the input data and return to the Javascript application in text/plain or text/json .

0
source

All Articles