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

I'm trying to print to a file, but this isn't working. How would I print multiple lines of html to a file w/o having to do a print statement for each line of html?
open(PB_BODY_LIMITED, "> $file_name") or die "Can't $!\n"; print PB_BODY_LIMITED " <table width=600> <tr> <th colspan='2' class='top'>-- Perbill Search Results --</th> </tr> <tr> <th class='left'>Company</th> <td>$data[0]</td> </tr> <tr> <th class='left'>Company ID(302)</th> <td>$company_name_id</td> </tr> <tr> <th class='left'> Docs</th> <td>$data[1]</td> </tr> <tr> <th class='left'> Bytes</th> <td>$data[2]</td> </tr> <tr> <th class='left'>From</th> <td>$from_month$from_day$from_year</td> </tr> <tr> <th class='left'>To</th> <td>$to_month$to_day$to_year</td> </tr> </table> ";

qball~"I have node idea?!"

Replies are listed 'Best First'.
Re: print multiple lines
by lhoward (Vicar) on May 15, 2001 at 23:13 UTC
    Probably the most common method is heredoc strings:
    print F <<EOT; Here's one line and another line and even some "quoted" data... EOT
    I like to use EOT as my "here doc" string, but you can use anything you want to.
Re: print multiple lines
by blue_cowdawg (Monsignor) on May 15, 2001 at 23:24 UTC

    This is something that I do in one of my tools that I wrote:

    open(FOUT,"> somefile.html" or die "$!"; print FOUT qq( <table width="770" cellpadding=0 ... -------8< snip! 8<-----------------

    other variants of this I have seen is to store the intended output in a single variable and just print the variable. There are many ways to do this.

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Peter L. Berghold --- Peter@Berghold.Net "Those who fail to learn from history are condemned to repeat it."
Re: print multiple lines
by MeowChow (Vicar) on May 15, 2001 at 23:19 UTC
    Read perldata, search for the term "here-document" in the section on scalar value constructors.
                   s aamecha.s a..a\u$&owag.print
Re: print multiple lines
by damian1301 (Curate) on May 15, 2001 at 23:34 UTC
    Just to add, it isn't working because everything is taken literally in double quotes. So, this means that you have to backslash everything that you want to show up (</tr> should be <\/tr>). There are many other ways to print stuff like that. You can use here documents, qq, q, or just use a similarly long statement something like...:

    print "<TR><TD>hi hi hi hi",'</tr></tD>';

    Note that variation of quotes there. This will work as expected and print out the </tr></td> as you expected, without the Perl interpreter's interaction.

    UPDATE: I now look at it and think I am wrong. But, I do now that you can always print HTML the right way, with CGI.pm, of course :-). Other than that I do not know what is wrong...

    Tiptoeing up to a Perl hacker.
    Dave AKA damian

      / prints fine... it is \ that is a problem...
                      - Ant
        Hmm...my HTML seems to come out funny when I put a / in there.

        Tiptoeing up to a Perl hacker.
        Dave AKA damian

Re: print multiple lines
by petdance (Parson) on May 16, 2001 at 00:01 UTC
    There are also modules to take care of this kind of thing. Take a look at this node where I give sample code for creating a form and table. Generating your code programattically guarantees that you don't miss any closing tags, and that your HTML is structured properly. It's worth a look, and it makes stuff much easier to maintain, and easier to see the page logic.


    %_=split/;/,".;;n;u;e;ot;t;her;c; ".   #   Andy Lester
    'Perl ;@; a;a;j;m;er;y;t;p;n;d;s;o;'.  #   http://petdance.com
    "hack";print map delete$_{$_},split//,q<   andy@petdance.com   >