Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: What is maintainable perl code?

by Juerd (Abbot)
on Sep 20, 2002 at 03:14 UTC ( [id://199396]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to What is maintainable perl code?

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://199396]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.