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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |