Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my Perl CGI program show the program code, not the output?

CODE - TEST.CGI

#!/usr/bin/perl  
use strict;  
use warnings;  

use CGI::FastTemplate;

my $tpl = new CGI::FastTemplate("/some/directory");  
$tpl->no_strict();  
$tpl->define(main    => "test.htm");  
$tpl->assign(TEST_CONTENT=> "Test");  
$tpl->parse(CONTENT   => "main");  
$tpl->print('CONTENT');  

TEMPLATE FILE

< html>  
< head>  
< title>TEST< /title>  
< /head>  

< body>  
$TEST_CONTENT  
< /body>  
< /html>

EXPLAIN

Why can't I see the desired output in a browser? When I navigate to the test.cgi file all I see is the actual code and not the template. What am I doing wrong?

like image 915
Luke Avatar asked Aug 24 '26 17:08

Luke


1 Answers

You are seeing code instead of the output of the program because you haven't configured you webserver to execute the program, so it is defaulting to serving the file as text/plain.

How you configure it depends on the server software you use. For example, see the Apache 2.2 CGI docs.

Second, the shebang line is missing. The program should start with:

#!/usr/bin/perl

Where /usr/bin/perl is the path to the Perl executable you wish to use.

Additionally, and not contributing to the problem:

  • You are missing use strict; and use warnings;. There should be boilerplate in any Perl program you are using as they catch many problems.
  • Your HTML document has no Doctype, so it triggers quirks mode. A suitable Doctype should be boilderplate in any HTML document you write.
like image 96
Quentin Avatar answered Aug 26 '26 20:08

Quentin