LPM: filtering
Frank Price
fprice at mis.net
Tue Mar 7 12:49:14 CST 2000
Hi LexPM,
I want to text-wrap a string to 50 columns before printing it to
sendmail. In the shell, I'd do this:
echo "Really long string ..." | fmt -50 | mail
How can I do the same in perl? I tried something like the following,
essentially from the Cookbook, but it just printed to my terminal's
stdout and then hung until I ctrl-D. I realize I could use a temp
array to hold the formatted output, but for some reason this seems
more "elegant" -- is there any way to make it work?
-Frank.
$width = 50;
$string = "Now is the time for all good men to come to the aid of \
their country! E Pluribus Unix, and all that! This is a very \
very very long string, ain't it?";
&fmt($string, $width);
open(SM, "| /usr/lib/sendmail -oi -t") or die "Can't open sendmail: $!\n";
print SM "To: blah at blah.com\n\n";
while (<>) {
print SM $_;
}
close STDIN;
close SM;
exit;
sub fmt {
my ($s, $w) = @_;
return if $pid = open(STDOUT, "|-"); # we're left w/ the child
die "Cannot fork: $!\n" unless defined $pid;
open(FMT, "| /usr/bin/fmt -$w") or die "Can't use fmt: $!\n";
print FMT $string;
close FMT;
exit;
}
More information about the Lexington-pm
mailing list