Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Template::Toolkit and Text::Table

by Skeeve (Parson)
on Feb 05, 2020 at 21:37 UTC ( #11112459=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I had the requirement to build a text table with Template::Toolkit. It's for the purpose of sending tabular data in a plain text mail.

As I couldn't find any module, I built my own TextTable filter.

An example table could look like this:

[% FILTER $TextTable %] >>ID<<|<<Name<<|>>Value>> ------+--------+--------- <<1>><<Bank ABC>><<5,740.00>> <<2>><<Super Fund>><<12,280.00>> <<1003>><<Big Savings Bank>><<430.00>> [% END %]

And the result would be

ID | Name | Value ----+----------------+--------- 1 |Bank ABC | 5,740.00 2 |Super Fund |12,280.00 1003|Big Savings Bank| 430.00

  • The << and >> in the header line define the alignment of the columns.
  • Spaces before and/or after the header texts define the alignment of the headlines. Centered is the default.
  • The separators are defined in the headline as well. Everything not between the << and >> is a separator.
  • The second line, if it does not contain any << and >>, is a rule. The first character will be the standard, the second will be used for "crossings".
  • The << and >> in the columns surround the values.

My Filter Plugin simply parses the text and translates it into Text::Table code.

I'm wondering whether or not the plugin could be useful for others, so my questions are:

  • Did I reinvent the wheel?
  • Do you have any suggestions for enhancements?
  • What do I have to do to make it a CPAN moduke?

Thanks for your attention.


s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re: Template::Toolkit and Text::Table
by Marshall (Canon) on Feb 06, 2020 at 10:11 UTC
    Perl has a built in formatter..see https://perldoc.perl.org/perlform.html

    I think you will find that it is more hassle than it is worth. I find that printf works a lot better.

    I also think that you will find that an email with the assumption that the user is using a fixed width font won't work well. Even if your email looks nice as you send it, the user's email program probably won't display it like you intended.

    Sounds like some kind of HTML doc will work better. Or perhaps a .pdf file?

      Perl has a built in formatter..see https://perldoc.perl.org/perlform.html

      But for this I would need to know the data upfront. I want the table to be built according to the data. In spite of my examle the data of course, comes from a database and the column widths haveto adjust. That's what Text::Table does.

      I also think that you will find that an email with the assumption that the user is using a fixed width font won't work well.

      I know. An HTML alternative is sent as well.


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
        Another option to consider is a .CSV file. Text::CSV is one possibility. Just a thought. Spreadsheet programs are pretty ubiquitous nowadays.A long, long time ago I had to produce a weekly report. I wound up making an Excel spreadsheet with some macros which I gave my users. Then the weekly report was a simple .CSV file (plain text) that they imported into my predeployed "smart sheet" (binary).
Re: Template::Toolkit and Text::Table
by Anonymous Monk on Feb 05, 2020 at 22:41 UTC
Re: Template::Toolkit and Text::Table
by Anonymous Monk on Feb 06, 2020 at 14:22 UTC
    "Hey, submit it to CPAN right away!" That library is full of things that people have invented, put a little more time into than was strictly necessary, and then shared. I'm quite sure that others will find your invention just as useful as you did. "Thanks for sharing!"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11112459]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2020-09-27 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (142 votes). Check out past polls.

    Notices?