Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Multi-line Format problem

by nr0mx (Scribe)
on Nov 08, 2003 at 17:47 UTC ( #305563=perlquestion: print w/ replies, xml ) Need Help??
nr0mx has asked for the wisdom of the Perl Monks concerning the following question:

In the below piece of code, the newlines between the lines of the poems ( $cookie ) are ignored. How do I get the format to preserve them ? Is there any way to get the desired output without having to eval the format ?

#!/bin/env perl -w use strict; my $cookie="Climb the mountains\nand get their good tidings.\nNature's + peace will flow into you\nas sunshine flows into trees.\n"; my $author="-John Muir\n"; $: = "- "; format STDOUT = @||||||||||||||||||||||||||||||||||||||||||||||||| "------------------------------------------------" ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $cookie @|||||||||||||||||||||||||||||||||||||||||||| $author @||||||||||||||||||||||||||||||||||||||||||||||||| "------------------------------------------------" . write STDOUT;
The $: line is in there because perldoc says that it controls the characters on which format is allowed to split the line. However, it doesn't seem to prevent newlines being ignored if the lines in question are smaller than what the format will allow in a single line.

In the below fragment, the output comes out like this --

------------------------------------------------ Climb the mountains and get their good tidings. Nature's peace will flow into you as sunshine flows into trees. -John Muir ------------------------------------------------
I would like it to be this --
------------------------------------------------ Climb the mountains and get their good tidings. Nature's peace will flow into you as sunshine flows into trees. -John Muir ------------------------------------------------

Comment on Multi-line Format problem
Select or Download Code
Re: Multi-line Format problem
by BrowserUk (Pope) on Nov 08, 2003 at 18:15 UTC

    The relevant passage of perlform is

    Inside of an expression, the whitespace characters \n, \t and \f are c +onsidered to be equivalent to a single space. Thus, you could think o +f this filter being applied to each value in the format: $value =~ tr/\n\t\f/ /; The remaining whitespace character, \r, forces the printing of a new l +ine if allowed by the picture line.

    You can achieve the output you want by translating the \n's to \r's

    #!/bin/env perl -w use strict; my $cookie="Climb the mountains\nand get their good tidings.\nNature's + peace will flow into you\nas sunshine flows into trees.\n"; my $author="-John Muir\n"; $cookie =~ tr[\n][\r]; $: = "- "; format STDOUT = @||||||||||||||||||||||||||||||||||||||||||||||||| "------------------------------------------------" ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $cookie @|||||||||||||||||||||||||||||||||||||||||||| $author @||||||||||||||||||||||||||||||||||||||||||||||||| "------------------------------------------------" . write STDOUT; __END__ ------------------------------------------------ Climb the mountains and get their good tidings. Nature's peace will flow into you as sunshine flows into trees. -John Muir ------------------------------------------------

    Though a couple of other adjustments are required.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!
    Wanted!

      Thanks, that worked magnificiently!.

      However I notice something surprising -- if the width of the $cookie format line is increased beyond a certain limit ( 63 in the above example ), the last line is gobbled up completely when being printed!

      The perlform documentation says at the very end - "The remaining whitespace character, \r, forces the printing of a new line if allowed by the picture line.". Any idea if this is responsible for what's happening ?

      I can get by with adjusting the format width to the correct amount, but it would be nice to know the reason anyhow.

Re: Multi-line Format problem
by TheDamian (Priest) on Nov 08, 2003 at 22:30 UTC
    Alternatively, you could use the Text::Reform module:
    use Text::Reform; print form " -------------------------------------------------", " [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[", $cookie, " ||||||||||||||||||||||||||||||||||||||||||||", $author, " -------------------------------------------------";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://305563]
Approved by Courage
Front-paged by Courage
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-10-26 06:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (151 votes), past polls