Output a perl script as a HTML Document

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • markoj
    New Member
    • Jul 2007
    • 11

    Output a perl script as a HTML Document

    Hi
    How do I go about making a perl script print its output as a HTML document
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    What have you tried so far?

    - Miller

    Comment

    • markoj
      New Member
      • Jul 2007
      • 11

      #3
      hi my boss has asked me to write a perl script to gather information which I have done and it is running from the cmd line in windows no problem, but he wants the output as a html page this is a bit of a test for me as this will be my first program in perl but I dont know where to start I was trying to configure apache to run the script but he says that is putting more work on myself do I change the script to cgi format as he says he just wants the output as simple text html

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Hard to understand. Should the output be written to a file or displayed in a web browser? All you really need for a perl script to output something a web browser can read and parse as html is an http content-type header with a mime type, can be as simple as:

        print "Content-type: text/html\n\n";

        Comment

        • Pranjal9880
          New Member
          • Jun 2007
          • 5

          #5
          Originally posted by markoj
          Hi
          How do I go about making a perl script print its output as a HTML document
          try this

          $file = 'test.html';
          open(fd,">$file ");
          print fd "<html><bod y>";
          ............... ............
          .....your code here........... ........
          ............... ............... .........
          print fd"</html></body>";

          Comment

          • markoj
            New Member
            • Jul 2007
            • 11

            #6
            HI thanks that is what I am trying to do all right but nothing is working if anyone can suggest something else I would be very grateful

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Originally posted by markoj
              HI thanks that is what I am trying to do all right but nothing is working if anyone can suggest something else I would be very grateful
              Post your perl code.

              Comment

              • markoj
                New Member
                • Jul 2007
                • 11

                #8
                Hi here it is

                [CODE=perl]
                #!/C:\Perl\bin
                use warnings;
                use strict;

                print "Content-type: text/html\n\n";
                print "<HTML><HEA D>";
                print "<TITLE>NAS Disk</TITLE>";
                print "</HEAD>";
                print "<BODY><H2> Nas Disk capacity</H2>";
                print "</BODY></HTML>";


                # my $data_file = '/perly/nas01.txt';
                my $data_file = 'nas01.txt';
                open DATA, "$data_file " or die "can't open $data_file $!";
                my @array_of_data = <DATA>;

                foreach my $line (@array_of_data ) {
                if ($line =~ m/levels/i) {
                print "$line\n";
                }
                }

                foreach my $line (@array_of_data ) {
                if ($line =~ m/date/i) {
                print "$line\n";
                }
                }

                foreach my $line (@array_of_data ) {
                if ($line =~ m/hostname/i) {
                print "$line\n";
                }
                }

                foreach my $line (@array_of_data ) {
                if ($line =~ m/uptime/i) {
                print "$line\n";
                }
                }

                print "============== =============== =============== =============== =============== ======\n";

                foreach my $line (@array_of_data ) {
                if ($line =~ m/filesystem/i) {
                print "$line\n";
                }
                }

                foreach my $line (@array_of_data ) {
                if ($line =~ m/SQLsrv/i) {
                print "$line\n";
                }
                }

                print "============== =============== =============== =============== =============== ======\n";

                foreach my $lines (@array_of_data ) {
                if ($lines =~ m/(\d+)[%]/i) {
                my $val = $1 ;
                print "$lines";

                if ($val > 80 ) {
                print "Please look at this disk immediately\n\n ";
                } else {
                print "Disk levels are ok\n\n";
                }
                }
                }

                close (DATA);

                print fd"</html></body>";
                [/CODE]
                Last edited by miller; Jul 17 '07, 03:41 PM. Reason: Code Tag and ReFormatting

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  OK, your code is doing too much work, see if this helps.

                  Code:
                  #!/C:\Perl\bin
                  use warnings;
                  use strict;
                  
                  print "Content-type: text/html\n\n";
                  print "<HTML><HEAD>";
                  print "<TITLE>NAS Disk</TITLE>";
                  print "</HEAD>";
                  print "<BODY><H2>Nas Disk capacity</H2>";
                  
                  
                  # my $data_file = '/perly/nas01.txt';
                  my $data_file = 'nas01.txt';
                  open DATA, "$data_file" or die "can't open $data_file $!";
                  my @array_of_data = <DATA>;
                  close DATA;
                  
                  
                  foreach my $line (@array_of_data){
                     if ($line =~ m/levels/i){
                        print "$line\n";
                     }
                     elsif ($line =~ m/date/i){
                        print "$line\n";
                     }
                     elsif ($line =~ m/hostname/i){
                        print "$line\n";
                     }
                     elsif ($line =~ m/uptime/i){
                        print "$line\n";
                     }
                  }
                  
                  print "================================================== ==============================<br>\n";
                  
                  foreach my $line (@array_of_data){
                     if ($line =~ m/filesystem/i){
                        print "$line\n";
                     }
                     elsif ($line =~ m/SQLsrv/i){
                        print "$line\n";
                     }
                  }
                  
                  print "================================================== ==============================<br>\n";
                  
                  foreach my $lines (@array_of_data){
                     if ($lines =~ m/(\d+)[%]/i){
                        my $val = $1 ;
                        print "$lines";
                        if ($val > 80 ){
                           print "Please look at this disk immediately\n\n";
                        }
                        else {
                           print "Disk levels are ok\n\n";
                        }
                     }
                  }
                  print "</html></body>";
                  Keep in mind that \n has no affect when viewed in a browser. Add <br> before \n (<br>\n) if you need to break to a newline where you have \n in your output.

                  Comment

                  Working...