Re: formats output \r\n question
by shmem (Chancellor) on Jun 27, 2006 at 15:30 UTC
|
Set the special variable $\ to "\r\n". See The parable of the falling droplet.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] [Watch: Dir/Any] |
|
I tried this and it didn't seem to work. I also tried comenting the binmode line out as well. thanks
#!/usr/bin/perl -w
use strict;
my $name = 'name';
my $home = "town\r\n";
{
local $\= "\r\n";
binmode STDOUT;
format STDOUT =
@<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<< \r\n
$name, $home
.
write STDOUT;
}
| [reply] [Watch: Dir/Any] [d/l] |
|
Darn. Well, TIMTOWTDI.
You could do:
#!/usr/bin/perl -w
use strict;
my $name = 'name';
my $home = "town";
my $form = "\@<<<<<<<<<<<<<<<<<< \@<<<<<<<<<<<<<<<\r\n";
formline($form, $name, $home);
print $^A;
See perlvar, formline for fiddling with the formats. But maybe it would be just easier to postprocess?
#!/usr/bin/perl -w
use strict;
my $name = 'name';
my $home = 'town';
printknecht(); # let's fork us a scribe
write;
format STDOUT =
@<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<
$name, $home
.
sub printknecht {
return if my $pid = open(STDOUT, "|-"); # return if parent.
die "cannot fork: $!" unless defined $pid;
while (<STDIN>) {
chop;
print $_."\r\n";
}
exit;
}
See Playing with STDIN and STDOUT for some prosa around this technique.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] |
Re: formats output \r\n question
by clwolfe (Scribe) on Jun 28, 2006 at 05:16 UTC
|
Or you could pipe the output of your script through unix2dos.
But that's the lazy unix shell programmer in me coming out. Coming up with the right output to begin with is probably more right (as opposed to spawning a whole new process just for line endings :-)
| [reply] [Watch: Dir/Any] |
Re: formats output \r\n question
by ioannis (Abbot) on Jun 27, 2006 at 18:57 UTC
|
See perlrun; you can also use the -l option
for end-line processing. This will affect print() and write(),
but not funtions like printf() .
#/usr/bin/perl -lw | [reply] [Watch: Dir/Any] |
Re: formats output \r\n question
by Ieronim (Friar) on Jun 28, 2006 at 13:39 UTC
|
In perl 5.8.x you can use the following solution:
#!/usr/bin/perl
binmode STDOUT, ':crlf';
print STDOUT "town\n";
All newline characters will be converted to CRLF. | [reply] [Watch: Dir/Any] [d/l] |
Re: formats output \r\n question (@)
by tye (Sage) on Jun 28, 2006 at 05:54 UTC
|
format STDOUT =
@<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<@
$name, $home, "\r"
.
| [reply] [Watch: Dir/Any] [d/l] |
|
Did you test that? Doesn't work. If it did, the OPs solution had been working in the first place.
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] [Watch: Dir/Any] |
|
Yes, I did test it. I see now how my test was flawed. Thanks.
This is another alternative, but I agree that I don't see what the advantage of this is over printf:
binmode STDOUT;
while( ... ) {
my( $name, $home )= ...;
formline "@<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<\r", $name, $hom
+e;
print $^A, $/; $^A= "";
}
This also suggested another "solution" that works but is quite undesirable due to it being fragile: Just include an extra literal carriage return on the end of each of the format's "picture" line(s) in the script's source code.
| [reply] [Watch: Dir/Any] [d/l] |
|
Re: formats output \r\n question
by Anonymous Monk on Jun 28, 2006 at 14:16 UTC
|
Try it with HEX-Code 10 and 13 | [reply] [Watch: Dir/Any] |
Re: formats output \r\n question
by Moron (Curate) on Jun 28, 2006 at 15:02 UTC
|
People are probably more familiar with local $/ = 'modified input record separator';
and yet another way to skin this cat is to use its brother:
local $\ = chr(10) . chr(13);
| [reply] [Watch: Dir/Any] [d/l] [select] |