substr vs regex

Greg McCarroll greg at
Mon Sep 3 10:58:17 BST 2007

On Mon, Sep 03, 2007 at 10:26:12AM +0100, alex at wrote:
> might use a regex /(...)$/ or substr($variable, -3)
> personally I recommend the former because it is more readable, but others

it's not more 'readable' or rather more maintainable than the substr.

rather than just being subjective (although I'm right ;-)), the objective
test for maintainability would be to choose which was more specialized
for the job at hand. substr is clearly all about grabbing a sub string from
a given position - which is exactly what the task is about. whereas a
regex can do everything from parsing to sudoku. when you choose the more
specialised option the next programmer doesn't have to think as hard - and
these little wins all add up.

> have recommended substr to because you "dont need to bring out the big
> guns".

write the damn benchmark hippy ;-)

> (However no one could actually remember the substr function)

sounds like these people need to do a little C penance so they appreciate
their Perl more. ;-)


More information about the mailing list