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.


In reply to Re: Remove RTF Formatting by TomDLux
in thread Remove RTF Formatting by GotToBTru

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.