mailmeakhila has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I have a cgi script. When i run it in browser i have to create html files on the fly with the data that is populated. I also need to have links to those files where i can display those html files. I am using the below code but i dont know why html files are not created. Is this a permission issue.

open HTML, ">tmp/$filename"; print HTML "Content-Type: text/html\n\n"; print HTML "<HTML>\n"; print HTML "<head>\n"; print HTML "<title> Second page </title>"; print HTML "<br>"; print HTML "$t->{title}"; print HTML "<br>"; print HTML "$t->{phone}"; print HTML "<br>"; print HTML "</head>"; print HTML "</HTML>"; close HTML;
Update: I tried adding die to the open but that didnt help. Also i have removed the Content-Type header.

Replies are listed 'Best First'.
Re: Create a html page on fly
by GrandFather (Saint) on Mar 17, 2012 at 02:24 UTC

    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
      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.

        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";
Re: Create a html page on fly
by chromatic (Archbishop) on Mar 16, 2012 at 20:35 UTC
    Is this a permission issue.

    Probably, but you need to check the error from your open to make sure. Either add use autodie; to your program (near the use CGI; line) or:

    open HTML, ">", "tmp/$filename" or die "Cannot open '$filename': $!\n";

    Improve your skills with Modern Perl: the free book.

Re: Create a html page on fly
by JavaFan (Canon) on Mar 16, 2012 at 20:34 UTC
    i dont know why html files are not created
    Perl does, and it more than happy to tell you, if you ask for it.
    open HTML, ">", "tmp/$filename" or die "Failed to open file: $!";
    You may have to check your servers error log.
      How can i check server logs? Update: I was able to check the logs. Its a permission issue. I resolved it. But i am facing a new issue here. The html page that is being generated is not being displayed. I am getting 500 internal server error. Any help?

        It could be that you are creating the HTML files in an area where the server expects to find CGI scripts, so it's trying to run them as scripts and failing. Again, your web server logs should give you more information behind the 500 error.

        Aaron B.
        My Woefully Neglected Blog, where I occasionally mention Perl.

Re: Create a html page on fly
by tobyink (Canon) on Mar 16, 2012 at 21:07 UTC

    In addition to what everybody's already said...

    print HTML "Content-Type: text/html\n\n";

    The above prints an HTTP header which has no business being in an HTML file on the filesystem. Ditch it.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Create a html page on fly
by Riales (Hermit) on Mar 16, 2012 at 20:36 UTC
    Try adding or die "Cannot open $filename" to the end of that open line:
    open HTML, ">tmp/$filename" or die "Cannot open $filename";
    EDIT: Yeah, what chromatic and JavaFan already covered...