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

I have a simple script that display a telephone list from a flat-file text database. Basically, I'm spitting out a *pre* tag, then printing line by line, and adding a */pre* tag at the end but I've noticed that I can't get the telephone numbers to line up. I really don't want to use table (they impede copying from the web page).

I found the FORMAT command in a reference book but they only illustrate how to use it at the command prompt and not any cgi interaction. Where can I find the proper syntax to get this to work under the web?

Replies are listed 'Best First'.
Re: FORMATting
by btrott (Parson) on Mar 29, 2000 at 23:18 UTC
    You should be able to use format just fine in a CGI script. Just print out the Content-Type header before you start printing out the HTML page as usual. Call the format STDOUT, then you can just use the write function to print out the data. Something like this:
    #!/usr/local/bin/perl -w use strict; use vars qw/$name $phone/; use CGI; my $query = new CGI; print $query->header; format STDOUT = @<<<<<<<<<<< @|||||||||||||| $name, $phone . my %phones = ( "Baz" => "1-293-593-2529", "Foo Bar" => "1-392-592-0382" ); print "<pre>\n"; print "Phone Numbers:\n"; for $name (keys %phones) { $phone = $phones{$name}; write; } print "</pre>\n";
    You'll want to mess with it a bit, but that's the general idea.
RE: FORMATting
by chromatic (Archbishop) on Mar 29, 2000 at 20:19 UTC
    To expand on turnstep's answer, the only way you'll get the numbers to line up is if you can control the font used to display them. There are two types of fonts -- proportional and non-proportional (or fixed-width). With the former, an i character is much narrower than a w. In a fixed-width font, all characters have the same width.

    This is important when writing CGI applications with Perl for one simple reason -- you don't control the font in the browser. That said, using the <PRE> or <CODE> tags will cause web browsers to use a fixed-width font to display the data. Now you know why. (And now you can use formats, if you want!)

RE: FORMATting
by turnstep (Parson) on Mar 29, 2000 at 20:02 UTC

    This is really a HTML question, not a perl one. Basically, PRE is your best bet. Every browser displays things differently, so there are never any guarantees that things will line up the way you want them. If it's just numbers, then PRE *should* be lining things up correctly. Tables, of course, will force things to line up, but it can be a pain. (Use of the &nbsp; character to simulate the extra spaces is not recommended - it will not line up for most browsers) You could also try using the <CODE> tag which causes most browsers to use a fixed-width font, which will line up quite nicely.

Re: FORMATting
by stephen (Priest) on Mar 30, 2000 at 04:24 UTC
    To find info about forms...

    At any UNIX prompt, type:

    man perlform
    

    Or, better yet, click here: perlman:perlform

    stephen