slojuggler has asked for the wisdom of the Perl Monks concerning the following question:

Using the following code if I run the enclosed code, the Hellos stay on the same line. If I change the Hello to Hello<space>, it wraps fine. I've tried Text::Wrapper and have gotten similar results.

How can I get the Hellos<sans spaces> to wrap correctly? Is it possible with this module, or do I need to roll my own solution?

The real story: The Hellos are simulating a super-long filename path(greater than 60 characters). I'd like to wrap the super-long filename so that it spans multiple lines.

Thanks for your help!

#!/usr/bin/perl -w #Text wrapping snippet use Text::Format; $text = new Text::Format; $text->columns(65); print $text->format("Hello" x 190);

Replies are listed 'Best First'.
Re: Text::Format Wrapping
by Rich36 (Chaplain) on Mar 04, 2003 at 22:17 UTC

    A viable solution is to use Text::Wrap

    use Text::Wrap; $Text::Wrap::columns = 60; my $text = "Hello" x 190; # Unwrapped print qq($text\n\n); # Wrapped print wrap('', '', $text), "\n\n";

    «Rich36»
Re: Text::Format Wrapping
by Cody Pendant (Prior) on Mar 04, 2003 at 23:09 UTC
    I guess what you need to do is read the docs on what Text::Format's options are when faced with unbroken strings of alpha characters, longer than the wrapping width.

    Like here for instance, what should PerlMonks do with this:

    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

    What Text::Wrap does is covered nicely in its documentation, I know that. You've got the option to force a wrap, leave the string as it is, or die.

    Which do you want?
    --

    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
Re: Text::Format Wrapping
by Hofmator (Curate) on Mar 05, 2003 at 12:05 UTC
    RTFM:
    BUGS Line length can exceed columns specified if columns is set to a small number and long words plus leading whitespace exceed column length specified. Actually I see this as a feature since it can be used to make up a nice word list.

    -- Hofmator

      Hi Hof:

      I did read that little caveat there. That's why I posted here. <grin>

      The rest: I would use Text::Wrap, however I'm using ActivePerl. The Visual Package Manager and the ppm are unable to find Text::Wrap using the installers.

      It's odd that Text::WrapProp is available through VPM and ppm, and yet I see Text::Wrap AND WrapProp at cpan.org.

      At this point, I'm trying to: 1) Figure out if I can do this using Text::Format or Text::Wrapper. 2) Figure out if it's possible to install this module without having to install cygwin/make/whatever else just to make the Text::Wrap module.

      Ah, the joys of Win32!

        Text::Wrap is a core module in 5.6 and 5.8, so no need to install it. Just use Text::Wrap!

        -- Hofmator