in reply to Splitting long text for Template

Hi Bod,

Making use of Template's EVAL_PERL constructor option :

use strict; use warnings; use Template; my $tt = Template->new({ EVAL_PERL => 1 }); my $str = 'This is too verbose to be printed in its entirety'; # 16 characters here ^ $tt->process(\*DATA, { string => $str }) || die $tt->error; __DATA__ Original string : [% string %] Formatted string : [%- PERL -%] if ( length '[%- string -%]' > 16 ) { (my $truncated = '[%- string -%]') =~ s/^(.{0,16}\b).*$/$1.../ +s; print $truncated; } else { print '[% string %]'; } [%- END -%]
Output:
Original string : This is too verbose to be printed in its entirety Formatted string : This is too ...

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Splitting long text for Template
by Bod (Parson) on Feb 08, 2021 at 18:00 UTC
    Hope this helps!

    It does a lot thanks 1nickt.
    Seeing an example can be very illuminating