3

I have this text I am writing in a Perl CGI program:

$text = $message;
@lines = split(/\n/, $text);
$lCnt .= $#lines+1;
$lineStart = 80;
$lineHeight = 24;

I want to force a return after 45 characters. How do I do that here?

Thanks in advance for your help.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
seeker7805
  • 67
  • 2
  • 7
  • possible duplicate of [How can I word wrap a string in Perl?](http://stackoverflow.com/questions/956379/how-can-i-word-wrap-a-string-in-perl) – Ether Oct 18 '10 at 17:18
  • This answers "how. do I emulate unix fmt in perl?" That explicit question should, I think, be mentioned in this document somewhere, so that people who have that question will be led to this document. I went searching with that question, came up with nothing pertinent, posted the question to stackoverflow with examples, and my question was quickly "closed" and referred here. – Jacob Wegelin Mar 22 '21 at 19:16
  • The exact title was "in perl, how can I emulate unix fmt?" Fortunately, that question now points the reader to this post. – Jacob Wegelin Mar 22 '21 at 19:32

4 Answers4

13

Look at the core Text::Wrap module:

use Text::Wrap;
my $longstring = "this is a long string that I want to wrap it goes on forever and ever and ever and ever and ever";
$Text::Wrap::columns = 45;
print wrap('', '', $longstring) . "\n";
CanSpice
  • 34,814
  • 10
  • 72
  • 86
1

Check out Text::Wrap. It will do exactly what you need.

Cfreak
  • 19,191
  • 6
  • 49
  • 60
1

Since Text::Wrap for some reason doesn't work for the OP, here is a solution using a regex:

my $longstring = "lots of text to wrap, and some more text, and more "
               . "still.  thats right, even more. lots of text to wrap, "
               . "and some more text.";

my $wrap_at = 45;

(my $wrapped = $longstring) =~ s/(.{0,$wrap_at}(?:\s|$))/$1\n/g;

print $wrapped;

which prints:

lots of text to wrap, and some more text, and 
more still.  thats right, even more. lots of 
text to wrap, and some more text.
Eric Strom
  • 39,821
  • 2
  • 80
  • 152
  • 3
    A couple of edge cases to consider: (1) What happens if you've got a string of more than 45 letters without a space? (2) Given a sequence of spaces that crosses the boundary, should the spaces at position 45 and up go at the beginning of the new line, or the end of the old one, or be dropped altogether? – Jander Oct 15 '10 at 03:06
0

The Unicode::LineBreak module can do more sophisticated wrapping of non-English text (Especially East Asian scripts) than Text::Wrap, and has some nice features like optionally being able to recognize URIs and avoid splitting them.

Example:

#!/usr/bin/env perl
use warnings;
use strict;
use Unicode::LineBreak;

my $longstring = "lots of text to wrap, and some more text, and more "
               . "still.  thats right, even more. lots of text to wrap, "
  . "and some more text.";

my $wrapper = Unicode::LineBreak->new(ColMax => 45, Format => "NEWLINE");
print for $wrapper->break($longstring);
Shawn
  • 47,241
  • 3
  • 26
  • 60
  • Just a little bit belated, but another question was just closed as a duplicate of this one, so... – Shawn Mar 22 '21 at 12:00