Search code examples
perl

How to direct output of Text::Table::More to a text file


I am able to output a table generated with the module Text::Table:Boxed as per example given here, the first lines being as follows:

binmode STDOUT, ":utf8";

use Text::Table::Boxed;
open(my $tfh, "> /home/zmumba/Output/Rtable.txt") or die "open 'Rtable.txt': failed $! ($^E)";
my $tb = Text::Table::Boxed->new({
... table contens ...
})

print $tfh $tb
close($tfh)

Text::Table::More as given here, does not clearly offer the possibility of directing output to a file. The first lines

#!perl
use 5.010001;
use strict;
use warnings;
use Text::Table::More qw/generate_table/;

my $rows = [
...Table content ...
];

binmode STDOUT, "utf8";
print generate_table(
.. attributes ...
);

I am able to generate the table to screen. I do not see how I get this table to a file.


Solution

  • The generate_table function returns a string.

    Like in the first code example, open a filehandle and print to it:

    open(my $tfh, "> /home/zmumba/Output/Rtable.txt") or die "open 'Rtable.txt': failed $! ($^E)";
    print $tfh generate_table(
        #... attributes ...
    );
    

    This worked for me using the SYNOPSIS code from Text::Table::More