Wrap text blocks in either a file or from STDIN. Nothing fancy or smart here, just some simple functional code.

Update: now deals with indentation correctly

#!/usr/bin/perl use strict; use Text::Wrap; use IO::File; die "Usage: wrap.pl FILE [WIDTH]\n" if @ARGV == 0 or $ARGV[0] =~ /^--?h/; $Text::Wrap::columns = ( @ARGV == 2 and $ARGV[1] =~ /^\d+$/ ) ? pop @ARGV : 80; if( $ARGV[0] eq '-' ) { print process_data( \*STDIN ); exit(0); } my $fn = shift @ARGV; my $fh = IO::File->new($fn, 'r+') or die("wrap.pl: $! - [$fn]\n"); my @wrapped = process_data($fh); $fh->seek(0, 0); $fh->truncate( 0 ); $fh->print( @wrapped ); $fh->close() or die("wrap.pl: $! - [$fn]\n"); exit(0); sub process_data { my $fh = shift; my @data = map { chomp; $_ } <$fh>; my $initial_indent = $data[0] =~ /^(\s+)/ ? $1 : ''; my $subsequent_indent = $data[1] =~ /^(\s+)/ ? $1 : ''; s/^\s*// for @data; return fill( $initial_indent, $subsequent_indent, @data ); } __END__ =pod =head1 NAME wrap.pl - basic wrapper around C<Text::Wrap>'s C<fill()> function =head1 USAGE wrap.pl filename $COLNUM The first argument can either be a filename or C<-> to indicate that input will be from C<STDIN>. The second argument (optional) will be the column width which the text will be wrapped at. =head1 TODO =over 6 =item * Use C<Pod::Usage> and the like =back =cut

In reply to Small wrapper for Text::Wrap's fill() by broquaint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.