home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl502b.zip / lib / Text / Wrap.pm < prev   
Text File  |  1995-12-28  |  2KB  |  94 lines

  1.  
  2. package Text::Wrap;
  3.  
  4. #
  5. # This is a very simple paragraph formatter.  It formats one 
  6. # paragraph at a time by wrapping and indenting text.
  7. #
  8. # Usage:
  9. #
  10. #    use Text::Wrap;
  11. #
  12. #    print wrap($initial_tab,$subsequent_tab,@text);
  13. #
  14. # You can also set the number of columns to wrap before:
  15. #
  16. #    $Text::Wrap::columns = 135; # <= width of screen
  17. #
  18. #    use Text::Wrap qw(wrap $columns); 
  19. #    $columns = 70;
  20. #    
  21. #
  22. # The first line will be printed with $initial_tab prepended.  All
  23. # following lines will have $subsequent_tab prepended.
  24. #
  25. # Example:
  26. #
  27. #    print wrap("\t","","This is a bit of text that ...");
  28. #
  29. # David Muir Sharnoff <muir@idiom.com>
  30. # Version: 9/21/95
  31. #
  32.  
  33. =head1 NAME
  34.  
  35. Text::Wrap -- wrap text into a paragraph
  36.  
  37. =head1 SYNOPSIS
  38.  
  39.     use Text::Wrap;
  40.     
  41.     $Text::Wrap::columns = 20; # Default
  42.     print wrap("\t","",Hello, world, it's a nice day, isn't it?");
  43.  
  44. =head1 DESCRIPTION
  45.  
  46. This module is a simple paragraph formatter that wraps text into a paragraph
  47. and indents each line. The single exported function, wrap(), takes three
  48. arguments. The first is included before the first output line, and the
  49. second argument is included before each subsequest output line. The third
  50. argument is the text to be wrapped.
  51.  
  52. =head1 AUTHOR
  53.  
  54. David Muir Sharnoff <muir@idiom.com>
  55.  
  56. =cut
  57.  
  58. require Exporter;
  59.  
  60. @ISA = (Exporter);
  61. @EXPORT = qw(wrap);
  62. @EXPORT_OK = qw($columns);
  63.  
  64. BEGIN    {
  65.     $Text::Wrap::columns = 76;  # <= screen width
  66. }
  67.  
  68. use Text::Tabs;
  69. use strict;
  70.  
  71. sub wrap
  72. {
  73.     my ($ip, $xp, @t) = @_;
  74.  
  75.     my $r;
  76.     my $t = expand(join(" ",@t));
  77.     my $lead = $ip;
  78.     my $ll = $Text::Wrap::columns - length(expand($lead)) - 1;
  79.     if ($t =~ s/^([^\n]{0,$ll})\s//) {
  80.         $r .= unexpand($lead . $1 . "\n");
  81.         $lead = $xp;
  82.         my $ll = $Text::Wrap::columns - length(expand($lead)) - 1;
  83.         while ($t =~ s/^([^\n]{0,$ll})\s//) {
  84.             $r .= unexpand($lead . $1 . "\n");
  85.         }
  86.     } 
  87.     die "couldn't wrap '$t'" 
  88.         if length($t) > $ll;
  89.     $r .= $t;
  90.     return $r;
  91. }
  92.  
  93. 1;
  94.