Hi Mike,
You asked:
Show quoted text> I am switching from using perl5's format to Perl6::Form and one thing I
> haven't figured out how to do is truncate a field like format's '...'
> ellipsis field definition. Is there a way to do this with form?
Yes, but it's not built-in.
Perl6::Form has an extension mechanism that makes adding new fields
easy (for sufficiently hard values of "easy"). The appended code
demonstrates how to add "ellipsis breaking".
Note that the break_with_ellipsis() subroutine and the 'use
Perl6::Form {field...'
code could be placed in a separate module, which you could then just load
when needed to enable the new feature.
Hope this helps,
Damian
-----cut----------cut----------cut----------cut----------cut----------cut----------cut----------cut-----
use 5.010;
use warnings;
use Perl6::Form;
sub break_with_ellipsis {
my ($breaker) = @_;
# This generates the replacement line-breaking subroutine...
return sub {
my ($str_ref, $width, $ws) = @_;
# Remember where we started this broken line...
my $start_pos = pos $$str_ref;
# Break it with the original breaker....
my ($nextline, $more) = $breaker->($str_ref, $width, $ws);
# If not at end-of-string...
if ($more) {
# If there's no room for the ellipsis...
if (length $nextline > $width-3) {
# Reset line to be (re)broken...
pos $$str_ref = $start_pos;
# Rebreak it with room for ellipsis...
($nextline, $more) = $breaker->($str_ref, $width-3, $ws);
}
# Add the ellipsis...
$nextline .= '...';
}
return ($nextline, $more);
}
}
use Perl6::Form {
# Specify a new kind of field...
field => [
# Match fields with trailing ellipses...
qr/ [{] [^}]* \.\.\. [}] /x
# And do this with them...
=> sub {
my ($match, $opts) = @_;
# Temporarily wrap breaking algorithm with ellipsis appender...
$opts->{break} = break_with_ellipsis( $opts->{break} );
# Rewrite format (removing ellipses from it)...
my $fieldtype = substr($match,1,-4);
return '{' . $fieldtype . substr($fieldtype,-1) x 3 . '}';
}
]
};
# Test it...
my $text = 'Now is the winter of our discontent made glorious summer
by this son of York';
print form
'|---------------------------|',
' {<<<<<<<<<<<<<<<<<<<<<<...}',
$text,
' {VVVVVVVVVVVVVVVVVVVVVVVVV}',
'|---------------------------|';