Using the shell's "fmt" command in a perl script

R

Ranyart

I have a variable containing about 200 characters of text, named
$text.

I would like to run the shell command "fmt" against this text, and
print out the output.

I have tried various combinations of the system and exec functions,
but so far, I have been frustrated.

Can anyone post a code snippet of the syntax I should use?

Much thanks!!!
 
T

Tad McClellan

Ranyart said:
I have a variable containing about 200 characters of text, named
$text.

I would like to run the shell command "fmt" against this text, and


Doing it in native Perl is nearly always better than "shelling out".

What does "fmt" do that Text::Wrap or Text::Autoformat can't do?

print out the output.

I have tried various combinations of the system and exec functions,
but so far, I have been frustrated.


You need to write to fmt's stdin, and read from fmt's stdout.

Doing one or the other is straightforward using backticks or
a pipe open().

Doing both is a whole different ballgame.


See the answer given in this Perl FAQ:

How can I open a pipe both to and from a command?
 
D

Danijel

Ranyart said:
I would like to run the shell command "fmt" against this text, and
print out the output.

Can anyone post a code snippet of the syntax I should use?

open(P, "| fmt") or die($!)
print P $text;
close(P) or die($!);

bye,
Da.Ta
 
S

Shawn Corey

Ranyart said:
I have a variable containing about 200 characters of text, named
$text.

I would like to run the shell command "fmt" against this text, and
print out the output.

I have tried various combinations of the system and exec functions,
but so far, I have been frustrated.

Can anyone post a code snippet of the syntax I should use?

Much thanks!!!

Untested:
open OUT, "| fmt" or die $!;
print OUT $text;
close OUT;

Also see: perldoc Text::Wrap
 
J

James Willmore

I have a variable containing about 200 characters of text, named
$text.

I would like to run the shell command "fmt" against this text, and
print out the output.

I have tried various combinations of the system and exec functions,
but so far, I have been frustrated.

Can anyone post a code snippet of the syntax I should use?

TMTOWTDI - the idiomatic Perl way or the way you you wanted to do it.

The way you wanted to do it requires you to save the output from the
script (in this case $text) to a file, then use fmt. fmt works better
if it has a real file to deal with.

Idiomatic Perl has several flavors to chose from. The one you may be
looking for uses formats (to make $text look uniform and make sure the
text is only so many collums wide, etc.)

For example:
==untested==
#!/usr/bin/perl -w

use strict;

my $text = 'The quick brown fox jumped over the lazy Perl
programmerXXXXX';
$text .= 'The quick brown fox jumped over the lazy Perl
programmerXXXXX';
$text .= 'The quick brown fox jumped over the lazy Perl
programmerXXXXX';
$text .= 'The quick brown fox jumped over the lazy Perl
programmerXXXXX';

format STDOUT=
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
$text
..

write;

exit;
==end==

Notice there are no newlines anywhere in $text. I take it that's why
you wanted to use fmt.

perldoc perlform

for more information.

HTH
 
J

James Willmore

On 30 Aug 2003 16:27:15 -0500
The way you wanted to do it requires you to save the output from the
script (in this case $text) to a file, then use fmt. fmt works
better if it has a real file to deal with.

Scratch that - it's not required - as illistrated from the other posts
to this thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top