Problems with format (perlform) and carriage returns... 
Author Message
 Problems with format (perlform) and carriage returns...

I saw that someone posted this question about 5 years ago, but there
was no answer (perhaps a personal reply?), so here goes:

I'm trying to use the following snippet of code

***Snip***
$details = "1\n2\n3\n4\n5\n";

$details =~ s/\n/\r/g;

open (REL_NOTES, ">relnotes") or die ("cannot open: $!");
select (REL_NOTES);
&relnotes;
close (REL_NOTES);

sub relnotes {
        $~ = "RELNOTES";
        write;

Quote:
}

format RELNOTES =

Details
====================================
~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$details

.
***/Snip***

What I get is a single line with the number '1' for the variable.
What I expected is:

1
2
3
4
5

Now, I saw in the documentation that \r is supposed to create a
newline in this situation.  I know that newlines are stripped, and I
need both the format of this number of characters AND some way of
"bleeding through" the carriage returns within the variable that I'm
printing.

Ideas, thoughts, suggestions?  

BTW, if an answer to this already exists somewhere in the newsgroup,
I'd appreciate a pointer to this knowledge :)



Tue, 02 Mar 2004 04:30:19 GMT  
 Problems with format (perlform) and carriage returns...

Quote:

>I saw that someone posted this question about 5 years ago, but there
>was no answer (perhaps a personal reply?), so here goes:

>I'm trying to use the following snippet of code

>***Snip***
>$details = "1\n2\n3\n4\n5\n";

>$details =~ s/\n/\r/g;

>open (REL_NOTES, ">relnotes") or die ("cannot open: $!");
>select (REL_NOTES);
>&relnotes;
>close (REL_NOTES);

>sub relnotes {
>        $~ = "RELNOTES";
>        write;
>}

>format RELNOTES =

>Details
>====================================
>~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>$details

>.
>***/Snip***

>What I get is a single line with the number '1' for the variable.

My guess is that is what you _see_, but that is not what you
are getting...

Quote:
>What I expected is:

>1
>2
>3
>4
>5

>Now, I saw in the documentation that \r is supposed to create a
>newline in this situation.  

Providing a copy/paste from the documentation would have
been better than paraphrasing it. Where in the docs are
you speaking of?

"newline" generally means the ASCII "line feed" character.

The ASCII "carriage return" character is a different character.

Quote:
>I know that newlines are stripped, and I
>need both the format of this number of characters AND some way of
>"bleeding through" the carriage

Why are you messing with CR line endings? Are you on a Mac?

Quote:
>returns within the variable that I'm
>printing.

>Ideas, thoughts, suggestions?  

Are you looking at the output at the command line? A CR is
_supposed_ to ... well, return the carriage (ie. go to beginning
of line). So the "2" overwrites the "1", the "3" overwrites the "2"...

Redirect the output to a file and look at the file size.

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Tue, 02 Mar 2004 05:04:27 GMT  
 Problems with format (perlform) and carriage returns...
On 13 Sep 2001 20:30:19 -0700,

Quote:
> I saw that someone posted this question about 5 years ago, but there
> was no answer (perhaps a personal reply?), so here goes:

> I'm trying to use the following snippet of code

> ***Snip***
> $details = "1\n2\n3\n4\n5\n";

> $details =~ s/\n/\r/g;

> open (REL_NOTES, ">relnotes") or die ("cannot open: $!");
> select (REL_NOTES);
> &relnotes;

This is a bit oldfashioned, and could be something else than you mean.
Subroutines without arguments are normally called as relnotes().

Quote:
> close (REL_NOTES)

> sub relnotes {
>         $~ = "RELNOTES";

If you name your format the same as the file handle, this isn't
needed.

Quote:
> format RELNOTES =

> Details
>====================================
> ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
> $details

> .
> ***/Snip***

> What I get is a single line with the number '1' for the variable.
> What I expected is:

> 1
> 2
> 3
> 4
> 5

Maybe you can use this instead:

$details = "1\n2\n3\n4\n5\n";
write;

format STDOUT =
Details

$details
.

Quote:
> Now, I saw in the documentation that \r is supposed to create a
> newline in this situation.

This whole area looks a bit buggy to me.  Watch:

$ perl
$details = "1\r2\r3\r4\r5\r";
write;
format =
^<<<<<<<<<<<<<<<<<~~
$details
.
__END__
1

$ perl
$details = "1\r2\r3\r4\r5\r";
$details .= "abcd " x 3;
write;
format =
^<<<<<<<<<<<<<<<<<~~
$details
.
__END__
1
2
3
4
5

$ perl
$details = "1\r2\r3\r4\r5\r";
$details .= "abcd " x 4;
write;
format =
^<<<<<<<<<<<<<<<<<~~
$details
.
__END__
1
2
3
4
5
abcd abcd abcd
abcd

$ perl
$details = "1\r2\r3\r4\r5\r";
$details .= "abcd\r" x 3;
write;
format =
^<<<<<<<<<<<<<<<<<~~
$details
.
__END__
1
2
3
4
5

$ perl                        
$details = "1\r2\r3\r4\r5\r";
$details .= "abcd\r" x 4;
write;
format =
^<<<<<<<<<<<<<<<<<~~
$details
.
__END__
1
2
3
4
5
abcd
abcd

Now, what this is all about, I can't tell you, but it certainly isn't
the correct behaviour.

Quote:
> Ideas, thoughts, suggestions?  

I'd abandon formats. They seem to be infrequently used, even though
they can be terribly useful, which may explain why these sorts of bugs
creep in, and exist for a long time (5.004_04, 5.005_03 and 5.6.1 all
behave like this). If I were at home I could check perl 4 as well...

Martien
--
Martien Verbruggen              |
Interactive Media Division      | Make it idiot proof and someone will
Commercial Dynamics Pty. Ltd.   | make a better idiot.
NSW, Australia                  |



Tue, 02 Mar 2004 05:31:43 GMT  
 Problems with format (perlform) and carriage returns...
On Fri, 14 Sep 2001 04:04:27 GMT,

Quote:

>>I saw that someone posted this question about 5 years ago, but there
>>was no answer (perhaps a personal reply?), so here goes:
>>Now, I saw in the documentation that \r is supposed to create a
>>newline in this situation.  

> Providing a copy/paste from the documentation would have
> been better than paraphrasing it. Where in the docs are
> you speaking of?

last few lines of perlform.

# man perlform
[snip]
       Inside of an expression, the whitespace characters \n, \t
       and \f are considered to be equivalent to a single space.
       Thus, you could think of this filter being applied to each
       value in the format:

        $value =~ tr/\n\t\f/ /;

       The remaining whitespace character, \r, forces the print-
       ing of a new line if allowed by the picture line.

Quote:
> "newline" generally means the ASCII "line feed" character.

> The ASCII "carriage return" character is a different character.

>>I know that newlines are stripped, and I
>>need both the format of this number of characters AND some way of
>>"bleeding through" the carriage

> Why are you messing with CR line endings? Are you on a Mac?

Because the perlform documentation says that that's what you need to
do if you want newlines in the output :)

Quote:
> Are you looking at the output at the command line? A CR is
> _supposed_ to ... well, return the carriage (ie. go to beginning
> of line). So the "2" overwrites the "1", the "3" overwrites the "2"...

> Redirect the output to a file and look at the file size.

In the example code, he wrote directly to a file.

See my post for some more test on this. What goes into the format
picture is 0x0d, what comes out is 0x0a (on unix, I suppose on MS it
would be different). There definitely is some problem here.

Martien
--
Martien Verbruggen              |
Interactive Media Division      | If at first you don't succeed,
Commercial Dynamics Pty. Ltd.   | destroy all evidence that you tried.
NSW, Australia                  |



Tue, 02 Mar 2004 05:44:41 GMT  
 Problems with format (perlform) and carriage returns...
[snip]

Quote:
> Now, I saw in the documentation that \r is supposed to create a
> newline in this situation.  I know that newlines are stripped, and I
> need both the format of this number of characters AND some way of
> "bleeding through" the carriage returns within the variable that I'm
> printing.

> Ideas, thoughts, suggestions?

Perhaps the Text::Reform module can do better?

--
"I think not," said Descartes, and promptly disappeared.



Tue, 02 Mar 2004 08:09:10 GMT  
 Problems with format (perlform) and carriage returns...
Thank you everyone for the comments and suggestions!  This is my first
post to this group, and I'm glad to see so many helpful people.
Please see below for my comments...

Quote:
> > open (REL_NOTES, ">relnotes") or die ("cannot open: $!");
> > select (REL_NOTES);
> > &relnotes;

> This is a bit oldfashioned, and could be something else than you mean.
> Subroutines without arguments are normally called as relnotes().

I'm actually maintaining someone else's code here, and they weren't
the best in style :(

Quote:
> Maybe you can use this instead:

> $details = "1\n2\n3\n4\n5\n";
> write;

> format STDOUT =
> Details
> ====================================

> $details
> .

I'd love to use this, but I really need the line to be specific
length, thus the series of '<'s.  I know another work around is to
replace all of the '\n's with a token character, than write the
format, then go back and replace the token with '\n's.  If the method
I'm trying to use worked, though, it'd be exactly what I need :)

Quote:
> Now, what this is all about, I can't tell you, but it certainly isn't
> the correct behaviour.

Is there someone I should notify that this appears to be a bug?  As a
programmer myself, I usually like to make sure that even though
something appears to just 'not work', I like to at least insure that
something somewhere else isn't affected by the bug (if this is one).


Tue, 02 Mar 2004 16:14:26 GMT  
 Problems with format (perlform) and carriage returns...
On 14 Sep 2001 08:14:26 -0700,

Quote:

>> Now, what this is all about, I can't tell you, but it certainly isn't
>> the correct behaviour.

> Is there someone I should notify that this appears to be a bug?  As a
> programmer myself, I usually like to make sure that even though
> something appears to just 'not work', I like to at least insure that
> something somewhere else isn't affected by the bug (if this is one).


should probably use the perlbug program, which comes with perl, but you
should make sure it sends the bug report to the right place).

If you feel unsure about how to do this, send me an email, and I'll
compile the report, with the examples I sent in the earlier post, and
submit it.

Martien
--
Martien Verbruggen              | The Second Law of Thermodenial: In
Interactive Media Division      | any closed mind the quantity of
Commercial Dynamics Pty. Ltd.   | ignorance remains constant or
NSW, Australia                  | increases.



Wed, 03 Mar 2004 01:33:11 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Carriage Return Problem

2. newline and carriage return problems

3. Carriage Return Problem

4. hard carriage return problem

5. New Line (carriage return/line feed) Problem

6. Carriage Return\line feed problem

7. Help remove carriage return after from a character string

8. mirror.pl - how to make it add carriage returns

9. File compare ignoring carriage return/line feed

10. Carriage Returns (ARG!)

11. Removing carriage returns from a string under certain conditions

12. Net::Telnet - command WITHOUT carriage return?

 

 
Powered by phpBB® Forum Software