in reply to Create a html page on fly

and as a style thing if you only have trivial amounts of HTML you could:

my $path = "tmp/$filename"; open my $htmlOut, '>', $path or die "Can't create $path: $!\n"; print $htmlOut <<HTML; <HTML> <head> <title> Second page </title><br> $t->{title}<br> $t->{phone}<br> </head> </HTML> HTML close $htmlOut;

which makes the HTML a lot easier to see. Note too the use of three parameter open and lexical file handles.

If you have larger lumps of HTML you should think about using one of the templating modules. My pick for light weight HTML templating is HTML::Template. Your code would look like:

use strict; use warnings; use HTML::Template; my $template = <<HTML; # Normally in an external file <HTML> <head> <title> Second page </title><br> <TMPL_VAR name="title"><br> <TMPL_VAR name="phone"><br> </head> </HTML> HTML my $tmpl = HTML::Template->new(scalarref => \$template); $tmpl->param(title => "Sir Robin", phone => '555-123-45678'); print $tmpl->output();

and print:

<HTML> <head> <title> Second page </title><br> Sir Robin<br> 555-123-45678<br> </head> </HTML>

The idea here is that it is much easier to maintain the HTML without worrying about the code that finally generates it, and much easier to maintain the code without worrying about the HTML.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Create a html page on fly
by mailmeakhila (Sexton) on Mar 19, 2012 at 15:22 UTC
    Hi

    I have tried your suggestion. The page gives a 500 internal server error. The scenario is i have different pages in my main page. Each pages in main page has a link and when clicked has to display the specific page. But now when i click it , its giving a 500 internal server error. The page is being displayed properly when i open it individually. Any suggestions would be really helpful. Thank you Akhila

    open my $htmlout, ">tmp/$filename" or die "Cant open: $!\n"; print $htmlout "<HTML>\n"; print $htmlout "<HEAD><TITLE>\n"; print "$i page"; print $htmlout "</TITLE>\n"; print $htmlout "<BODY>\n"; for my $t (@{$res->{result}}) { print $htmlout "$t->{title}<br>"; print $htmlout "$t->{phone}<br>"; } print $htmlout "</BODY></HEAD></HTML>\n"; close $htmlout;

      "Any suggestions would be really helpful."

      What does the web server error log say? See also Ovid's CGI Course.

        How can i achieve this? My main page is already a cgi page. Should i be adding another print statement with Content-Type?
      Once you have created the HTML file you need to print the location of the new file as a URL, something like:
      ... close $htmlout; my $url = "http://example.com/directory/$filename"; print "Location: $url\n\n";