Maintainable Perl code is not different from any other maintainable code. While it is true that a single character may behave like what would be ten lines in another language, that doesn't make the program less maintainable. In fact, I think a single character is a lot easier to maintain.

It's easier to maintain 150 lines of Perl than the 500-line C equivalent. Perl does more in a single line, so don't expect to read 5 lines per second.

As an example, let's take a piece of DBD::mysql:

static int CountParam(char* statement) { char* ptr = statement; int numParam = 0; char c; while (c = *ptr++) { switch (c) { case '"': case '\'': /* * Skip string */ { char end_token = c; while ((c = *ptr) && c != end_token) { if (c == '\\') { ++ptr; if (*ptr) { ++ptr; } } else { ++ptr; } } if (c) { ++ptr; } break; } case '?': ++numParam; break; default: break; } } return numParam; }
The Perl version of the same would probably be something like (untested):
# Count question marks that are not in (escapable) quotes sub CountParam { my ($statement) = @_; return scalar grep $1 eq '?', $statement =~ /( \? # Question mark | "(?:\\.|[^\\"]+)*" # String in \-escapable "" | '(?:\\.|[^\\']+)*' # String in \-escapable '' | [^?"']+ # Anything else )/sgx; }
The more statements you have, the more statements can be malfunctioning. On the other hand, removing one character from this regex may break it.

Is Perl code maintainable? IMHO, it is. Is it readable? If you know Perl, it is -- I admit: the C version is easier for Perl coders than the Perl version is for C coders.

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.


In reply to Re: What is maintainable perl code? by Juerd
in thread What is maintainable perl code? by disciple

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.