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

Discussion in 'Perl Misc' started by Ranyart, Aug 30, 2003.

  1. Ranyart

    Ranyart Guest

    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!!!
    Ranyart, Aug 30, 2003
    #1
    1. Advertising

  2. 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



    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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Aug 30, 2003
    #2
    1. Advertising

  3. Ranyart

    Danijel Guest

    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
    Danijel, Aug 30, 2003
    #3
  4. Ranyart

    Shawn Corey Guest

    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
    Shawn Corey, Aug 30, 2003
    #4
  5. On 30 Aug 2003 16:27:15 -0500
    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?


    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
    James Willmore, Aug 30, 2003
    #5
  6. On Sat, 30 Aug 2003 22:27:18 GMT
    James Willmore <> wrote:
    > On 30 Aug 2003 16:27:15 -0500
    > Ranyart <> 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
    James Willmore, Aug 30, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek Mahar
    Replies:
    0
    Views:
    2,896
    Derek Mahar
    Aug 27, 2003
  2. klikic

    JSTL fmt:setLocale problem

    klikic, Apr 28, 2006, in forum: Java
    Replies:
    0
    Views:
    4,657
    klikic
    Apr 28, 2006
  3. jill
    Replies:
    0
    Views:
    1,021
  4. tempsl

    Implementing Unix fmt ( ) function

    tempsl, Nov 16, 2005, in forum: C Programming
    Replies:
    0
    Views:
    417
    tempsl
    Nov 16, 2005
  5. moongeegee

    execute a shell script in a shell script

    moongeegee, Dec 3, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    237
    Ben Morrow
    Dec 4, 2007
Loading...

Share This Page