![]() |
Using the shell's "fmt" command in a perl script
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!!! |
Re: Using the shell's "fmt" command in a perl script
Ranyart <spamvictim@dontdoit.com> wrote:
> 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? -- Tad McClellan SGML consulting tadmc@augustmail.com Perl programming Fort Worth, Texas |
Re: Using the shell's "fmt" command in a perl script
Ranyart wrote:
> 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 |
Re: Using the shell's "fmt" command in a perl script
Ranyart wrote:
> 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 |
Re: Using the shell's "fmt" command in a perl script
On 30 Aug 2003 16:27:15 -0500
Ranyart <spamvictim@dontdoit.com> wrote: > 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 -- Jim |
Re: Using the shell's "fmt" command in a perl script
On Sat, 30 Aug 2003 22:27:18 GMT
James Willmore <jwillmore@cyberia.com> wrote: > On 30 Aug 2003 16:27:15 -0500 > Ranyart <spamvictim@dontdoit.com> wrote: > 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. -- Jim |
| All times are GMT. The time now is 01:36 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.