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

Hi...
Can somebody tell me why the following sript dont print the content file correctly:
use CGI qw(:standard); use strict; use warnings; open(INPUT, "dtdversao3.dtd") or die "$!"; my @array=<INPUT>; close(INPUT); my $i=0; print header; print start_html('A Simple Example'), do{ $array[$i], $i=$i+1; }while($i<30); print end_html;
the result in monitor is:
1 2 3 id CDATA #REQUIRED 5 Name CDATA #REQUIRED 6 > 7 8 9 10 id CDATA +#REQUIRED 12 Name CDATA #REQUIRED 13 > 14 15 16 id CDATA #REQUIRED 18 + Name CDATA #REQUIRED 19 > 20 21 22 23 id CDATA #REQUIRED 25 Name CDA +TA #REQUIRED 26 > 27 28 29 30

Replies are listed 'Best First'.
Re: print file in cgi with perl
by dga (Hermit) on Jul 23, 2003 at 16:02 UTC

    Your do is passed along to print as a list which takes the list, inserts a space between the items, and prints it out. Also since you are assiging a value to $i it is passed along to print to be printed also.

    Additionally, you are loading the entire file into your programs memory space and then tossing all but the first 30 lines.

    #starting from the open print header; print start_html('A Simple Example'); my $i=0; while(<INPUT>) { $i++; last if $i>30; print; } close(INPUT); print end_html;

    I tried to make this example similar in spirit to the original. This method only reads the first 30 lines and only uses memory for one at a time. The file you are printing is not html though so it will still not look right at all in a browser. You may want:

    print header('text/plain'); #the while here #no start or end html prints

    This will print the contents as a text file in which the line breaks and spacing from the file will be preserved.

      I use the apache 2.0.46 server for windows2000 and the IE5.
      The problem is that the browser dont print anything.
      But in the "view source" there are all i want.
      So, the problem can be the header type.
(jeffa) Re: print file in cgi with perl
by jeffa (Bishop) on Jul 23, 2003 at 15:56 UTC
    because this:
    do{ $array[$i], $i=$i+1; }while($i<30);
    doesn't print anything. Replace that block with this one line:
    print for @array;
    But first fix this line:
    print start_html('A Simple Example'),
    You need a semi-colon at the end, not a comma. ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      Since print prints a list, you don't even need the for:

      print @array;

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      Note: All code is untested, unless otherwise stated

      This solution dont resolve my problem.
      Now dont print anything in monitor.
      #!C:/server/Perl/bin/perl.exe use CGI qw(:standard); use XML::Checker::Parser; use strict; use warnings; my $checker = new XML::Checker; open(INPUT, "dtdversao3.dtd") or die "$!"; my @array=<INPUT>; close(INPUT); my $i=0; print header; print start_html('A Simple Example'); print for @array; print end_html;
        Are you sure about that? :) Remember, DTD's get parsed by the browser - try viewing the source.

        You probably should use dga's excellent suggestion and

        print header('text/plain');
        instead.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)