Your skill will accomplishwhat the force of many cannot PerlMonks

### Re^3: scalar or sub that can tell me the terminal print offset ?

by Not_a_Number (Prior)
 on Mar 17, 2012 at 19:51 UTC ( #960207=note: print w/replies, xml ) Need Help??

I must agree with LanX that you probably have an XY Problem. Have you considered Text::Wrap?

• Comment on Re^3: scalar or sub that can tell me the terminal print offset ?

Replies are listed 'Best First'.
Re^4: scalar or sub that can tell me the terminal print offset ?
by ww (Archbishop) on Mar 17, 2012 at 20:50 UTC
Not_a_Number hits it squarely on the head, as best /me can tell from the question and addendum.

"Text::Wrap" should be fairly easy to modify to suit your rules -- once you've decided what all of them are.

But if you're a glutton for punishment, you could modify this exercise (which dates back several years and is rather fragile -- tabs in the data, for example, would break the formatting for the lines in which they appeared) -- likely by outputting the \$chars to another string, and reporting its len.

```#!/usr/bin/perl
use strict;
use warnings;

my (\$content, @content);
{
local \$/;
\$content = <DATA>;
}

@content = split //, \$content;

my \$content_ref = \@content;

count( \$content_ref );

sub count {
print "         1         2          3         4         5
+ 6\n";
print "12345678901234567890012345678901234567890123456789012345678
+90\n";
my \$char;
my \$count = 0;
my \$string_ref = shift;

my @string = @\$string_ref;

for \$char ( @string ) {
if ( (  \$count  > 50 ) && ( \$char ne ' ' ) ) {
print \$char;
++\$count;
next;
}elsif ( (  \$count  > 50 ) && ( \$char eq ' ' ) ) {
print "\$char\n";
\$count = 0;
next;
} elsif ( \$char =~ /\n/ ) {
print "\$char";
\$count = 0;
next;
} else {
print \$char;
++\$count;
}
}
}

__DATA__
data:

This is a paragraph that will be far too wide to print on a single lin
+e in a normal width terminal (console) so we are going to count chars
+ and tell Perl to insert a newline after a reasonable number of chars
+, sixty, in this case.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Praesent ant
+e metus, iaculis a, adipiscing nec, consequat et, arcu. Proin massa.
+Nulla dictum, leo ac elementum scelerisque, felis ipsum porta justo,
+a viverra orci sem ut diam. Integer facilisis volutpat orci. Donec at
+ turpis sed purus ultricies rhoncus. Fusce urna. Praesent ac ligula i
+d augue interdum consectetuer. Sed vestibulum, dui eu dignissim tinci
+dunt, mi orci eleifend metus, tempus placerat arcu nulla sit amet lac
+us. Duis felis. Aenean lobortis turpis a elit. Suspendisse mi quam, t
+empus ac, venenatis in, commodo sed, enim. Proin dignissim placerat l
+acus.

Maecenas blandit est et ipsum. In dignissim suscipit leo. Ut congue mi
+ nec dolor. Curabitur malesuada purus congue purus. Nulla facilisi. M
+aecenas at felis. Aliquam cursus, risus vitae rutrum vestibulum, tell
+us ante pulvinar tortor, rutrum faucibus enim magna ac nisi. Proin li
+bero mauris, aliquam vel, luctus vitae, pretium eget, nisi. Etiam nun
+c neque, tempus malesuada, commodo non, tempor vel, sapien. Duis elem
+entum fermentum libero. In tristique. Curabitur pede neque, adipiscin
+g quis, egestas a, imperdiet ac, velit.
Output:
```         1         2          3         4         5         6
1234567890123456789001234567890123456789012345678901234567890
data:

This is a paragraph that will be far too wide to print
on a single line in a normal width terminal (console)
so we are going to count chars and tell Perl to insert
a newline after a reasonable number of chars, sixty,
in this case.

Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Praesent ante metus, iaculis a, adipiscing nec,
consequat et, arcu. Proin massa. Nulla dictum, leo ac
elementum scelerisque, felis ipsum porta justo, a viverra
orci sem ut diam. Integer facilisis volutpat orci. Donec
at turpis sed purus ultricies rhoncus. Fusce urna. Praesent
ac ligula id augue interdum consectetuer. Sed vestibulum,
dui eu dignissim tincidunt, mi orci eleifend metus,
tempus placerat arcu nulla sit amet lacus. Duis felis.
Aenean lobortis turpis a elit. Suspendisse mi quam,
tempus ac, venenatis in, commodo sed, enim. Proin dignissim
placerat lacus.

Maecenas blandit est et ipsum. In dignissim suscipit
leo. Ut congue mi nec dolor. Curabitur malesuada purus
congue purus. Nulla facilisi. Maecenas at felis. Aliquam
cursus, risus vitae rutrum vestibulum, tellus ante pulvinar
tortor, rutrum faucibus enim magna ac nisi. Proin libero
mauris, aliquam vel, luctus vitae, pretium eget, nisi.
Etiam nunc neque, tempus malesuada, commodo non, tempor
vel, sapien. Duis elementum fermentum libero. In tristique.
Curabitur pede neque, adipiscing quis, egestas a, imperdiet
ac, velit.

But plaudits to those others who also suspect an XY problem, too, as what you've specced so far, seems somewhat removed from real-world relevance. Or, is this homework? If so, say so, because you learn very little about programming if you hand in one of our solutions.

Create A New User
Node Status?
node history
Node Type: note [id://960207]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2018-04-23 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (84 votes). Check out past polls.

Notices?