diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-16 19:48:29 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-23 18:03:42 +0100 |
| commit | 95677ee25bcfc34fbfea5d5dc570e06f3872788c (patch) | |
| tree | 105a6574f7cfedbdb090e16849618102998c1d41 /challenge-019 | |
| parent | e72a0d33e191a62a83d69c6d4252fa733192ca77 (diff) | |
| download | perlweeklychallenge-club-95677ee25bcfc34fbfea5d5dc570e06f3872788c.tar.gz perlweeklychallenge-club-95677ee25bcfc34fbfea5d5dc570e06f3872788c.tar.bz2 perlweeklychallenge-club-95677ee25bcfc34fbfea5d5dc570e06f3872788c.zip | |
Challenge 019 task 2
Diffstat (limited to 'challenge-019')
| -rwxr-xr-x | challenge-019/jo-37/perl/ch-2.pl | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/challenge-019/jo-37/perl/ch-2.pl b/challenge-019/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..c3689c0472 --- /dev/null +++ b/challenge-019/jo-37/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl -s + +use v5.16; +use warnings; +use List::Util 'reduce'; + +our $textwidth //= 72; + +die <<EOS unless @ARGV; +usage: $0 [-textwidth=L] PARAGRAPH + +-textwidth=L + specify maximum line length + +PARAGRAPH + text taken as a single paragraph + +EOS + + +say reduce { + # match the last line. + $a =~/(.*)$/; + # Append the next word separated by a newline if the textwidth + # would be exceeded or a space otherwise. + $a . (length($b) + length($1) + 1 > $textwidth ? "\n" : " ") . $b; + } split /\s+/, shift; |
