An Example: Using the Text::Wrap Module

Here's a small example that uses a module from the standard library: the Text:Wrap module, which, given a very long string, will wrap that string into multiple lines of a given length, and with an optional indentation character to include for each line.

This particular example formats an input file to be 80 or so characters wide and indents it in a format familiar to you if you're used to e-mail—it puts a > symbol at the start of each line. The file to be quoted is assumed to be broken up into multiple paragraphs with a blank line between each one. So, for example, if the input file looks like this (a single long string; the end of lines here are not actual ends of lines in the input):

The event on which this fiction is founded has been supposed, by Dr. Darwin, and
some of the
physiological writers of Germany, as not of impossible occurrence. I shall not
be supposed as
according the remotest degree of serious faith to such an imagination; yet, in
assuming it as the
basis of a work of fancy, I have not considered myself as merely weaving a
series of supernatural
terrors. The event on which the interest of the story depends is exempt from the
disadvantages of
a mere tale of spectres or enchantment. It was recommended by the novelty of the
situations which
it develops; and, however impossible as a physical fact, affords a point of view
to the imagination
for the delineating of human passions more comprehensive and commanding than any
which the
ordinary relations of existing events can yield.
					

The output will look like this:

> The event on which this fiction is founded has been supposed, by Dr.
> Darwin, and some of the physiological writers of Germany, as not of
> impossible occurrence. I shall not be supposed as according the remotest
> degree of serious faith to such an imagination; yet, in assuming it as
> the basis of a work of fancy, I have not considered myself as merely
> weaving a series of supernatural terrors. The event on which the interest
> of the story depends is exempt from the disadvantages of a mere tale of
> spectres or enchantment. It was recommended by the novelty of the
> situations which it develops; and, however impossible as a physical
> fact, affords a point of view to the imagination for the delineating of
> human passions more comprehensive and commanding than any which the
> ordinary relations of existing events can yield.

Listing 13.2 shows the code for the script to do this.

Listing 13.2. The wrapit.pl Script
1:  #!/usr/ bin/perl -w
2:  use strict;
3:
4:  use Text::Wrap;                 # import module
5:  my $indent ="> ";              # indent character
6:
7:  while (<>) {
8:      print wrap($indent, $indent, $_);
9 : }

As you can see, this is not very much code at all, and it's much easier than writing the same procedure using raw Perl code. The important parts of this script are

  • Line 4, where we import the Text::Wrap module.

  • Line 5, which defines the indent character (here >, although it could be any set of indentation characters you want).

  • Line 8, where we call the wrap function to actually wrap the test. The wrap function, defined in the Text::Wrap module, takes three arguments: the character to indent the first line with, the character to indent each successive line with, and the string to wrap. In this case, we wanted to indent all the lines with the same character, so we called wrap with $indent specified twice.

By default, the Text::Wrap function wraps to 76 characters wide. You can change this value using the $columns variable, although that variable is not imported from the module by default. You'll have to import that variable explicitly or use its full package name to be able to make use of it:

use Text::Wrap qw($columns);   # import $columns
$columns = 50;                 # set it
					

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.40.4