#!/usr/local/bin/perl ############################################################################## # File: wrapper.pl # Purpose: Word wraps files so that the maximum line length is less # that the specified parameter. Words are defined by non- # white-space seperated by white-space. Words longer than # the maximum line length are *not* split into multiple lines. # Existing line breaks are preserved. # # Calling # Sequence: wrapper.pl [-l ] [] # # Where: is the maximum line length. (Default=80) # # is a list of file names (wild cards processed). If # file are not specified, input is assumed to be STDIN. # # output is written to STDOUT. # # Example: wrapper.pl -l 132 my.file # # Author: Tom Parris ############################################################################## require "getopts.pl"; # process the command line options &Getopts('l:') ; if(!$opt_l) { $line_length = 80 ; } elsif ($opt_l == 1) { die "wrapper.pl: error: -l switch requires a value.\n"; } else { $line_length = $opt_l ; } $length = 0 ; while (<>) { if( /^\s*$/ ) { print "\n\n" ; $length = 0 ; next ; } @words = split(/\s+/) ; while( $#words >= 0 ) { $word = shift(@words) ; $wlength = length($word) ; $newlength = $length + $wlength ; if ($newlength < $line_length) { print $word ; $length = $newlength ; } else { print "\n" ; print $word ; $length = $wlength ; } if ($length >= ($line_length-1)) { print "\n" ; $length = 0 ; } else { print " " ; $length++ ; } } }