Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Format question...

by khudgins (Sexton)
on Jan 07, 2003 at 19:14 UTC ( #225044=perlquestion: print w/replies, xml ) Need Help??

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

I'm working on the final bits of a new web store application for my employer, and I've run into a bit of a pickle. We need to send the customer an email receipt for their order, and we'd like to send them a nicely formatted table of the items in their cart.

I can't use HTML, as there's a company policy decision stating that we don't put HTML in emails. (I agree - I read much email via PINE myself, and I hate HTML in my inbox). Therefore, format is the way to go. I think.

My question is, how do I make a format against a loop? I'm pulling the data from my database, and it's all working fine in HTML::Template. So I get:

$sqlString = "SELECT * FROM CartItems WHERE txtSessId = ?"; $sth = $dbh->prepare($sqlString); $sth->execute($session) $cartItemsRef = $sth->fetchall_arrayref({});

for a great return set for a TMPL_LOOP in my HTML templates. I can't format those well for a text-based table - I've tried. Given a format like this:

format RECEIPT = Item Quantity Price -------------------------------------------- @<<<<<<<<<<<<<<<<<<<<< @<<<<< @####.## $vars,$go,$here .

How do I loop this on the $cartItemsRef? I don't understand the format system deeply enough to do this.

Replies are listed 'Best First'.
Re: Format question...
by Hofmator (Curate) on Jan 07, 2003 at 19:41 UTC
    Something like the following should work:
    format RECEIPT_TOP = Item Quantity Price -------------------------------------------- . format RECEIPT = @<<<<<<<<<<<<<<<<<<<<< @<<<<< @####.## @format_row . for my $row_ref ( @$cartItemsRef ) { @format_row = @$row_ref; write RECEIPT; # this writes to the filehandle RECEIPT }

    You can read up more on formats with perldoc perlform.

    And maybe you should consider another solution altogether like using a templating system, e.g. TT2 or Text::Template.

    Update: The code above only works if you retreive exactly these three columns (in the order you use them in the template) from the database. Otherwise the @format_row = ... line has to be adjusted accordingly.

    -- Hofmator

Re: Format question...
by pfaut (Priest) on Jan 07, 2003 at 19:36 UTC

    You might want to look into Text::Template.

    Regarding using format in a loop, you would just loop through the list of items returned from the database and do a write for each.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Format question...
by krujos (Curate) on Jan 07, 2003 at 19:32 UTC
    The easiest solution for you may be to have two formats. Or use the top of page format discussed here. You could have on format for the text that will appear on the top of the page and then one for the individual items and loop through the array writing each item.
    Best of luck. josh
Re: Format question...
by Mr. Muskrat (Canon) on Jan 07, 2003 at 19:50 UTC

    I'd start by reading the format() and write() sections of perlfunc, then move on to perlform.

    If you have access to Programming Perl, 3rd Edition, read chapter 7 Formats.

     

    I really hate it when I start to answer a question and do not get to finish writing it until much later...

      I read every bit of that and still didn't get a feel for it.

      I'm not sure why the docs didn't really melp me any. Hofmator's example above was exactly the kind of thing I was looking for. Thanks for all the quick replies, everyone!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://225044]
Approved by Mr. Muskrat
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2021-05-14 16:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (150 votes). Check out past polls.

    Notices?