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

Hi, I have a script written on linux m/c, Actually I wanted to print a HTML page in "/usr/anil/scripts/myhtml.html". With the below code the html file is not created "myhtml.html".


#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
sysopen (HTML, '/usr/anil/scripts/myhtml.html', O_RDWR|O_EXCL|O_CREAT, 0755);
printf HTML "<html>\n";
printf HTML "<head>\n";
printf HTML "<title>My Home Page</title>";
printf HTML "</head>\n";
printf HTML "<body>\n";
printf HTML "

Here we have an HTML page with a paragraph.

";
printf HTML "</body>\n";
printf HTML "</html>\n";
close (HTML);

Replies are listed 'Best First'.
Re: Creating HTML page
by Corion (Patriarch) on Aug 05, 2008 at 09:03 UTC

    You don't check whether your sysopen() call failed. Do that. Also, why are you using sysopen instead of using plain open?

    my $outname = 'usr/anil/scripts/myhtml.html'; open HTML, '>', $outname or die "Couldn't create '$outname': $!";
      Corion, I have tried with the lines you have suggested but still the thml file is NOT created...

      print "content-type: text/html \n\n"; #The header
      my $outname = 'usr/anil/scripts/myhtml.html';
      open HTML, '>', $outname || die "Couldn't create '$outname': $!";
      printf HTML "<html>\n";
      printf HTML "<head>\n";
      printf HTML "<title>My Home Page</title>";
      printf HTML "</head>\n";
      printf HTML "<body>\n";
      printf HTML "

      Here we have an HTML page with a paragraph.

      ";
      printf HTML "</body>\n";
      printf HTML "</html>\n";
      close ($outname);
        Others have mentioned your open should be using or, not ||.

        You seem to have lost the leading / from $outname. You should be using:

        my $outname = '/usr/anil/scripts/myhtml.html'; open HTML, '>', $outname or die "Couldn't create '$outname': $!";

        If that still doesn't work, tell us what the error message is.

        Why aren't you using strict and warnings? Always start your Perl programs with

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


        Unless I state otherwise, all my code runs with strict and warnings
        open HTML, '>', $outname || die "Couldn't create '$outname': $!";

        This is not what I suggested. I suggested what I suggested for a reason.

        Also, consider using <code>...</code> around your code, so that it renders and downloads nicely.

Re: Creating HTML page
by cdarke (Prior) on Aug 05, 2008 at 10:26 UTC
    Also, close ($outname); is incorrect in Perl, you should use the handle, as in your original post. I also suggest you use print rather than printf, it is supposed to be more efficient.

    However, I cannot see any reason why the file would fail to be created and no error reported. Double check your error checking. As a last resort run strace on the script:
    strace -o strace.out perl myscript.pl
    It will probably produce a lot of output in strace.out but sometimes gives information which is not obvious in a script.
      Oh nooo....lotss of errors when I added
      use strict;
      use warnings;
      to this script. Almost all the errors are Global symbol "$count" requires explicit package name at frame7.pl line 7.
        So, declare each of them when you first use them with my...
        my $outname = '...';


        Unless I state otherwise, all my code runs with strict and warnings
Re: Creating HTML page
by dHarry (Abbot) on Aug 05, 2008 at 11:17 UTC

    If you want to output html, why not use one of the Perl modules available on CPAN (for example HTML::Tiny to name just one). Doing this with printf statements seems like traveling backwards in time.

    A reply falls below the community's threshold of quality. You may see it by logging in.