Line wrapping non-printable characters

P

Paul Harman

I need to take a block of text and introduce line-wrapping at 78 chars. A
familiar requirement, one would think };*)

Having used format before, I started there. However I wanted to capture the
data into a variable rather than write a file. I couldn't get the formline
trick to work, so instead I used the
open F, ">", \$destvariable;
....trick instead which works wonderfully.

Unfortuantely my source material has non-printable characters, which format
and/or write seem to be stripping. My google-fu is not strong enough to find
a solution to this.

Can anyone help me out? I'm starting to resign myself to writing a regular
expression loop...

Paul
 
A

A. Sinan Unur

I need to take a block of text and introduce line-wrapping at 78
chars. A familiar requirement, one would think };*)

Having used format before, I started there. However I wanted to
capture the data into a variable rather than write a file. I couldn't
get the formline trick to work, so instead I used the
open F, ">", \$destvariable;
...trick instead which works wonderfully.

Unfortuantely my source material has non-printable characters, which
format and/or write seem to be stripping. My google-fu is not strong
enough to find a solution to this.

Can anyone help me out? I'm starting to resign myself to writing a
regular expression loop...


CPAN is your friend:

http://search.cpan.org/~muir/Text-Tabs+Wrap-2005.0824/lib/Text/Wrap.pm

Sinan
 
A

Anno Siegel

Paul Harman said:
I need to take a block of text and introduce line-wrapping at 78 chars. A
familiar requirement, one would think };*)

Having used format before, I started there. However I wanted to capture the
data into a variable rather than write a file. I couldn't get the formline
trick to work, so instead I used the
open F, ">", \$destvariable;
...trick instead which works wonderfully.

Unfortuantely my source material has non-printable characters, which format
and/or write seem to be stripping. My google-fu is not strong enough to find
a solution to this.

I don't know about format, I'll assume your diagnosis is correct.
Can anyone help me out? I'm starting to resign myself to writing a regular
expression loop...

Like this?

my $lines = join "\n", $block =~ /(.{0,78})/sg;

Doesn't look so bad to me. The one tricky part is to allow an empty match
({0,78}, not {1,78}). That supplies an empty string after all other matches,
which makes join() add a final line-feed.

Or do you have "original" line feeds to consider? Then split the block
on line-feed first, treat each part as above, and join them.

Anno
 
P

Paul Harman

Anno Siegel said:
Like this?

my $lines = join "\n", $block =~ /(.{0,78})/sg;


Yes, ish - but as you surmised later on I have existing line feeds to cope
with, and a few other wrinkles. But yeah it was effectively that simple to
roll my own.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,104
Latest member
LesliVqm09
Top