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

hi all, Can someone help with a simple request. I am trying to create a simple html file from my perl script.
At the moment I am opening a file and writing to it using the PRINT command. But when I view the html file, it displays nothing even though the html is there in the source
example of code
open(OUTPUT, $outputFile); # Send content-type print "content-type: text/html \n\n"; # OUPTUT Header Section # print OUTPUT "<html>\n"; print OUTPUT "<head>\n"; print OUTPUT "<title>Created By Perl<\title>\n"; print OUTPUT "</head>\n"; # HTML Body # print OUTPUT "<body>\n";
thanks for your time. UPDATE:
thanks guys, sorry for not being more clear, bit hot yesterday in London (UK) and think my brain was melting at the end of the day !
I have got the html file working now, just need to do some cosmetic stuff to make it look pretty... thanks again

Replies are listed 'Best First'.
Re: Create html file from script
by liverpole (Monsignor) on Jul 18, 2006 at 14:57 UTC
    Hi dtharby,

    Please be aware that you are attempting to open an INPUT file, (even though the filehandle is called OUTPUT).  That's because the default mode of open is "read".

    To open a file for output, you should do either:

    open(OUTPUT, ">$outputFile");

    Or even better...

    open(OUTPUT, ">", $outputFile);

    And best of all, because it checks for an error condition:

    open(OUTPUT, ">", $outputFile) or die "Failed to write to '$outputFile +' ($!)\n";

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      ++, Liverpole and ditto to explorer

      Code below is based on the guess that that OP is trying to create a static file ...and, at that, perhaps only on a non-server box:

      #!usr/bin/perl -w use strict; use vars qw($out); $out = "test060718.htm"; open( OUTPUT, ">", "$out") or die "Can't open $out \n"; # Send content-type -- NOT needed if sending to a file # print "content-type: text/html \n\n"; # OUTPUT Header Section # print OUTPUT "<html>\n"; print OUTPUT "<head>\n"; print OUTPUT "<title>Created By Perl</title>\n"; #\title fixed to /title as per [explorer] 's node, below print OUTPUT "</head>\n"; # HTML Body # print OUTPUT "<body>\n"; print OUTPUT "foo\n<h1>bar</h1>\n</body></html>\n";

      produces:

      <html> <head> <title>Created By Perl</title> </head> <body> foo <h1>bar</h1> </body></html>
Re: Create html file from script
by explorer (Chaplain) on Jul 18, 2006 at 14:59 UTC
    The problem is here:
    print OUTPUT "<title>Created By Perl<\title>\n";
    Perl interpret '\t' like a tab space.
    The solution is write the correct char: '/'
Re: Create html file from script
by davorg (Chancellor) on Jul 18, 2006 at 14:42 UTC

    It's not clear exactly what you are doing. Is this program running on a web server? Are you running it as a CGI program? Where are you writing the output file to?

    If (as I suspect) you're just running the program to create a local file and then opening the file in a browser then you don't need the CGI content type header. Try removing that.

    If you're running the program as a CGI program then you should be printing to STDOUT, not to a file.

    But it's hard to be much help without knowing a lot more about what you are doing.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Create html file from script
by philcrow (Priest) on Jul 18, 2006 at 14:43 UTC
    From the Content-type header, I'm assuming you are trying to return the html to a browser. In that case, don't print the html to a file, rather print it directly to standard out as you did for the header. However, I will also note that direct html generation is not very popular as it becomes difficult to maintain as the size of your problem grows. Therefore, most people turn to either a templating system (like Template, HTML::Mason, etc.) or CGI.pm to do the generation.

    Phil

Re: Create html file from script
by marto (Cardinal) on Jul 18, 2006 at 14:48 UTC
Re: Create html file from script
by Ieronim (Friar) on Jul 18, 2006 at 17:14 UTC
    Most errors mentioned above could be avoided, if you
    1. used warnings and strict (use warnings warns about "print on closed filehandle")
    2. used templates or at least here-documents
    3. output the HTML code to STDOUT (it makes debugging from a terminal much easier, and you can always redirect the output to chosen file))
    Look at the following snippet:
    use warnings; use strict; print "Content-Type: text/html\n\n"; #remove if you print to file print <<HTML; <html> <head> <title>Created By Perl</title> </head> <body> HTML __END__
    Modifying your code to look like it can be a good first step :)

         s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print
Re: Create html file from script
by explorer (Chaplain) on Jul 18, 2006 at 18:59 UTC
    The same, with CGI.pm:
    use CGI qw/:standard/; open OUTPUT,">$outputFile"; print OUTPUT header, start_html('Created By Perl'), h1('Created By Perl'), hr, p("If you see this text is that it is working well"), hr, end_html; close OUTPUT;