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

Hello Monks,
I don't exactly understand the '\r' character and how to make it work for me. I have some rather long strings that I output with the help of (good old) format. My strings have '\n' characters within them that I'd like to preserve while I output the entire string via the '~~ $var' convention of format.

The camel book says I'm effectively experiencing
$value =~ tr/\n\t\f/ /;
continuing with: The remaining whitespace character, \r, forces the printing of a new line if the picture line allows it.

Fine, how do I put this to work?

In the following (working) example, doing nothing (Version A) indeed simply replaces \n with a space character. (See output under __DATA__.) Version B might be tolerable, Version C is my feeble attempt at using the advice above. But what is the mystery method that will produce Version D (in which I can even tolerate the indented info)????

my $last_line_label = 'IMPRESSION/PLAN: '; my $line = 'I discussed otitis media with her but since her ears are +normal today, we do not need to do anything with them. _____________________________ Marcus Welby, M.D. MW/bnj'; format FLDFMT = @<<<<<<<<<<<<<<<<^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $last_line_label, $line ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $line . $~ = 'FLDFMT'; # # Version A $line =~ s/\n/' ' x 50/ge; # Version B #$line =~ tr/\n/\r/; # Version C #mystery_command; # Version D write; __DATA__ Version A: IMPRESSION/PLAN: I discussed otitis media with her but since her ears are normal today, we do not need to do anything with them. _____________________________ Marcus Welby, M.D. MW/bnj Version B: IMPRESSION/PLAN: I discussed otitis media with her but since her ears are normal today, we do not need to do anything with them. _____________________________ Marcus Welby, M.D. MW/bnj Version C: IMPRESSION/PLAN: I discussed otitis media with her but since her ears are normal today, we do not need to do anything with them. _____________________________ Marcus Welby, M.D. Version D: IMPRESSION/PLAN: I discussed otitis media with her but since her ears are normal today, we do not need to do anything with them. _____________________________ Marcus Welby, M.D. MW/bnj

Replies are listed 'Best First'.
Re: Using '/r' with '~~ $var' in format
by grep (Monsignor) on Nov 01, 2002 at 16:53 UTC
    I would suggest changing you approach, I think you are stuffing too much in that one line. You are doing format's job, when you try to control the layout with your own \r and \n's.

    #!/usr/bin/perl -w use strict; my $last_line_label = 'IMPRESSION/PLAN: '; my $line = 'I discussed otitis media with her but since her ear +s are normal today, we do not need to do anything with them.'; my $blank_line = '_____________________________'; my $sig = 'Marcus Welby, M.D.'; my $prep_by = 'MW/bnj'; format FLDFMT = @<<<<<<<<<<<<<<<<^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $last_line_label, $line ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $line @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $blank_line @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $sig @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $prep_by . $~ = 'FLDFMT'; write;



    grep
    Mynd you, mønk bites Kan be pretti nasti...
      Well, you are right that a practical answer is to change the approach. But there's stuff behind the question that you can't see ;-)

      Quoting Larry/The Camel Book,
      The truly desperate can generate their own format on the fly...

      When my program runs, I won't know what is in the $line string, i.e. it won't always be data types like sig and prep_by. (And I also build the FORMAT definition in a subroutine but I thought that would distract from my question.) Practically speaking, the data will usually be paragraphs with newlines for separators, and it's these blank lines that I particularly want respected. But as my Version D shows, I'd really like any newline within $line to be respected.

        What about ditching format? It sounds like your needs will be surpassing what format will do easily (and more importaintly if you do pull it off, it sounds like a maintainence nightmare).

        Template Toolkit sounds like a great fit for you. TT is designed to template any job not just HTML (as long as you don't need pagination)



        grep
        Mynd you, mønk bites Kan be pretti nasti...
Re: Using '/r' with '~~ $var' in format
by BrowserUk (Patriarch) on Nov 05, 2002 at 07:05 UTC

    This seems close to your Version D?

    Update:Unfortunately, the operative word in that sentance is "looks". The reality when tested with longer text is that it does no wrappong whatsoever. My apologies.

    I also got something that got closer by doing $line =~ s/\n/\r/g; and repeating the the last two lines in your original format a bunch of times. It wraps the long line ok and places the horizontal rule underneath, but then the rest of the sig just disappears into the ether. :(

    my $last_line_label = 'IMPRESSION/PLAN: '; my $line = 'I discussed otitis media with her but since her ears are +normal today, we do not need to do anything with them. _____________________________ Marcus Welby, M.D. MW/bnj'; format FLDFMT = @<<<<<<<<<<<<<<<<^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $last_line_label, $line @* $line . $~ = 'FLDFMT'; write; __END__ C:\test>209620 IMPRESSION/PLAN: I discussed otitis media with her but since her ears are normal today, we do not need to do anything with them. _____________________________ Marcus Welby, M.D. MW/bnj C:\test>

    Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
      I also got something that got closer by doing $line =~ s/\n/\r/g; and repeating the the last two lines in your original format a bunch of times. It wraps the long line ok and places the horizontal rule underneath, but then the rest of the sig just disappears into the ether. :(

      Yes, I tried /s and decided that the more efficient (?) tr would be just as well to present since they gave equally "not quite right" results. And BTW, it's cheating to repeat the last two lines of the format! :-) I have a little subroutine on the side that builds up the format string all by itself, i.e. no human intervention to say how many times to 'repeat' the lines. Although if some sample code shows how this is actually a solution, bring it on!

      The other thing is, in what appears to be output below the __END__ statement, we have lost the hanging block effect I have in mind for $line. The fact that my sample text ($line) with the newlines is a signature block is irrelevant; I'm just trying to build a general purpose format statement that plays out a variable that happens to have embedded newlines. Thanks though!