If it's going to remain under 20 lines, I have no problem with using the default $_.
But I've never known a problem to not develop exceptions, so you wind up with dozens or hundreds of lines of code. If it gets any more complicated, you should split code into subroutines, especially if some of the processing is conditional. In any case, is the verbose form slower than the default form?
Your first regex comment says remove lines starting with braces, but you don't enforce the starting part. I would personally process the file on a line-by-line basis, after all, you might get gigabyte submissions, and so would stop looking at a line as soon as I detected the curly brace. Note that \A is the beginning of the string, \z is the end of the string.
LINE:
while (my $line = <$INFILE> ) {
chomp
next LINE if $line =~ m<\A\s*{>;
#
# or if it was going to be literally
# the first char, with no spaces ...
#
next LINE if q<{> eq substr $line, 0, 1;
#
# other processing
#
$text .= $line . $NEWLINE;
}
Lines with only a slash are easy:
next LINE if length $line == 1 and q{\} eq substr $line, 0, 1;
In #3, when removing leading spaces ( You don't care about spaces within the line, it seems), instead of copying the newline and the stuff after the space, why not use Look-Around Assertions to specify you only want to remove spaces that come after a newline. No need to say anything about what follows, it will grab all spaces and tabs.
s/(?<=\n)\s+//;
#or, line by line:
s/\A\s+//
Similarly in #4, don't copy the line stuff, just drop trailing spaces
s/\s+\n//;
I would predefined a constant, or at least a variable, for the double backslash and newline.
Readonly my $ESC_BACKSLASH => q{\\};
Readonly my $NEWLINE => q{\n};
$wholefile =~ s/$ESC_BACKSLASH\z//o;
In #2, don't capture the RTF command you want to discard, it just slows things down. AS for the word boundary \b, it isn't really doing anything useful, because greediness will make the hyphenated word slurp up everything it can.
Readonly my $RTF_CMD => qr/$ESC_BACKSLASH [\w-]+/xo;
s/$RTF_CMD//;
As Occam said: Entia non sunt multiplicanda praeter necessitatem.
|