I'm trying make a simple Perl text table using Text::Table. I want pipes as the main column separator, hyphens for a horizontal rule, and plus signs where the rule crosses the column separator.
Here is a MWE:
use strict;
use warnings;
use Text::Table;
my $sep = \'│';
my $tb = Text::Table->new("Heading 1", $sep, "Heading 2", $sep, "Heading 3");
$tb->add("Row 1 Col 1", "Row 1 Col 2", "Row 1 Col 3");
$tb->add("Row 2 Col 1", "Row 2 Col 2", "Row 2 Col 3");
print $tb->title;
print $tb->rule('-','+');
print $tb->body;
I used the suggestion from the Text::Table documentation that "Another useful combo is $tb->rule( '-', '+'), together with separators that contain a single nonblank "|", for a popular representation of line crossings."
But it's giving me this output:
Heading 1 │Heading 2 │Heading 3
-----------+++-----------+++-----------
Row 1 Col 1│Row 1 Col 2│Row 1 Col 3
Row 2 Col 1│Row 2 Col 2│Row 2 Col 3
Notice there are three plus signs at each rule/column intersection rather than the expected one.
I've tested this in Text::Table 1.133 and 1.135 (latest).
What am I doing wrong?
When I copied your code and ran it, I got a strange character instead of the pipe (|) in the output. Also, I do not see 3 plus signs like you do; I only see 1.
I am using Text::Table version 1.135.
When I change the $sep variable to:
my $sep = \'|';
I see this output:
Heading 1 |Heading 2 |Heading 3
-----------+-----------+-----------
Row 1 Col 1|Row 1 Col 2|Row 1 Col 3
Row 2 Col 1|Row 2 Col 2|Row 2 Col 3
See also: How can I print a table with multi-line strings using the Text::Table module?
As shown in the previous answer, you have chosen a multibyte utf-8 character for the separator. The most general solution is to tell perl that your code is written in utf-8 encoding by adding the use utf8 pragma. Now if your output contains multibyte characters you will get 'wide character in print' warnings, so you should add the utf-8 encoding layer to STDOUT as well:
use strict;
use warnings;
use Text::Table;
use utf8;
use open qw(:std :encoding(UTF-8));
my $sep = \'│';
my $tb = Text::Table->new("Heading 1", $sep, "Heading 2", $sep, "Heading 3");
$tb->add("Row 1 Col 1", "Row 1 Col 2", "Row 1 Col 3");
$tb->add("Row 2 Col 1", "Row 2 Col 2", "Row 2 Col 3");
print $tb->title;
print $tb->rule('-','+');
print $tb->body;
This assumes, that you are in a utf-8 terminal of course. See perldoc open and perldoc utf8 for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With