http://www.perlmonks.org?node_id=11112459

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

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:

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!"