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

So after some reading, I have decided to go with the Text::Format module, it seems to be a pretty handy tool for my project. But I have, of course encountered a problem. Does anyone know how to replace the separator character? That is, instead of just one line, skip two lines(or any other character for that matter), i.e. double spaced. Is this even possible w/wrap? Here's what I am trying to accomplish:
____DATA_______ Here is one big long string which needs to have some formatting done t +o it before I can use it for input in a separate program. I would like it to be formatted exactly as follows (except for the exa +ct line length): ___OUTPUT______ [one tab]'Here is one big long string which '+ 'needs to have some formatting done '+ 'to it before I can use it for input '+ 'in a separate program' (no "+" on last line)
I have figured out how to get a tab in there, and the leading quote, but haven't figured out a way to add the '+ to the end of the lines. I'm pretty sure I need to do something with this: direct from CPAN<quote> If you want to separate your lines with something other than \n then set $Text::Wrap::separator to your preference. This replaces all newlines with $Text::Wrap::separator. If you just to preserve existing newlines but add new breaks with something else, set $Text::Wrap::separator2 instead.</quote>
I can't wrap my head around the syntax though. Is $Text a variable in the module? Can I set it to any value. I couldn't get it to work, got a very ambiguous 'useless setting to a void' or some such vagueness. Ideas?

Replies are listed 'Best First'.
Re: Text::Format Module q
by sauoq (Abbot) on Oct 11, 2005 at 01:04 UTC
    I can't wrap my head around the syntax though. Is $Text a variable in the module?

    No, $separator is a variable in the module. And, $Text::Wrap::separator is how you refer to that variable from other modules. And, yes, setting that to "+\n" should get you close to what you want.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Text::Format Module q
by BrowserUk (Patriarch) on Oct 11, 2005 at 02:06 UTC

    Fun. As a one-liner:

    perl -wlne"printf qq[\t'%s'\n], join qq['+\n\t'], m[(.{1,34}(?: |$)|.{34})] +g" junk.txt 'Here is one big long string which '+ 'needs to have some formatting done '+ 'to it before I can use it for '+ 'input in a separate program.' 'I would like it to be formatted '+ 'exactly as follows (except for the '+ 'exact line length)'

    Or with a configurable line length

    #! perl -slw use strict; for my $n ( 4 , 34, 70 ) { print "\n$n"; my $pos = tell DATA; while( <DATA> ) { chomp; printf "\t'%s'\n", join "'+\n\t'", m[(.{1,$n}(?: |$)|.{$n})]g; } seek DATA, $pos, 0; } __DATA__ Here is one big long string which needs to have some formatting done t +o it before I can use it for input in a separate program. I would like it to be formatted exactly as follows (except for the exa +ct line length)

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.