wordwrap function

H

hudson

Here's my idea for a wordwrap function:

Usage:
use lib "./";
use Modules::AddNewLines;
$output = add_new_lines($output);

Module:
---------------------------------------
package Modules::AddNewLines;
use strict;
use vars qw(@ISA @EXPORT $VERSION);

use Exporter;
$VERSION = 1.00; # Or higher
@ISA = qw(Exporter);

@EXPORT = qw(add_new_lines);

sub add_new_lines {
my $string = shift;
my $count = 1;
my $new_string;
while ($string =~ /(.)/g) {
$new_string .= $1;
if ($count >= 80 && $1 eq " ") {
$new_string .= "\n";
$count = 0;
}
$count++;
}
return $new_string;
}

1;
 
M

Matt Garrish

hudson said:
Well, I thought it was neat, but maybe someone has a few suggestions?

Your threshold for neatness is set too low.

Nothing to do while high school is out, or is there a reason why you want to
add newlines to files?

Matt
 
M

Matt Garrish

hudson said:
Here's my idea for a wordwrap function:

Usage:
use lib "./";
use Modules::AddNewLines;
$output = add_new_lines($output);

Module:
---------------------------------------
package Modules::AddNewLines;
use strict;
use vars qw(@ISA @EXPORT $VERSION);

use Exporter;
$VERSION = 1.00; # Or higher
@ISA = qw(Exporter);

@EXPORT = qw(add_new_lines);

sub add_new_lines {
my $string = shift;
my $count = 1;
my $new_string;
while ($string =~ /(.)/g) {
$new_string .= $1;
if ($count >= 80 && $1 eq " ") {
$new_string .= "\n";
$count = 0;
}
$count++;
}

One big problem with this code is that you aren't checking for existing
newlines. What if $string contains the entire contents of a slurped in file?
What if the paragraphs have already been broken up, but the user wants them
recombined and the line breaks to be standardized? What if the user wants
the line breaks at a different point?

The code has a very limited application as it's currently written. You need
to consider all the possibilities and write your code to accomodate them as
much as possible. Even if it's only for your own use, do you want to have to
go and modify the file each time you want to change the break length, or
would you rather be able to just specify an option in your code to set it
each time you call the subroutine?

Matt
 
H

hudson

One big problem with this code is that you aren't checking for existing
newlines. What if $string contains the entire contents of a slurped in file?
What if the paragraphs have already been broken up, but the user wants them
recombined and the line breaks to be standardized? What if the user wants
the line breaks at a different point?

The code has a very limited application as it's currently written. You need
to consider all the possibilities and write your code to accomodate them as
much as possible. Even if it's only for your own use, do you want to have to
go and modify the file each time you want to change the break length, or
would you rather be able to just specify an option in your code to set it
each time you call the subroutine?

Matt

well, I never thought of all that! thanks...I find perl really forces
you to generalize and I am trying, but I guess I need to generalize a
bit more
 
J

Jay Tilton

: Here's my idea for a wordwrap function:

Is there something you dislike about the Text::Wrap module?
 
H

hudson

: Here's my idea for a wordwrap function:

Is there something you dislike about the Text::Wrap module?

I tried it once and it seemed very slow...but I might have been
mistaked or tried to run some super huge file through it ;-)
 
H

hudson

Your threshold for neatness is set too low.
actually, it was in combination with a few things....storing a sub routine
in a hash and combining that with print...so being able to do:

print format_string($config{action}(split /\n/, $kv{words}));

seemed really neat to me...but sorry, I didn't really get that across in
my orignial post
 
J

Jay Tilton

: On Fri, 16 Jul 2004 00:34:21 GMT, (e-mail address removed) (Jay Tilton) wrote:
:
: >
: >: Here's my idea for a wordwrap function:
: >
: >Is there something you dislike about the Text::Wrap module?
:
: I tried it once and it seemed very slow...but I might have been
: mistaked or tried to run some super huge file through it ;-)

The Benchmark module can quantify speed differences. You will probably see
an insignificant difference between the two subs, even with the Text::Wrap
module's extra whizbangs.

Using s/// can provide results similar to your sub in a fraction of the
time.

sub regex_wrap {
(my $o = shift) =~ s/(.{79,}? )/$1\n/g;
$o;
}

Embedded linebreaks screw up both solutions, but in different ways. Is it
important to get a bad result as quickly as possible? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top