in reply to Re: regex 1st match only
in thread regex 1st match only

While I'm not among them, there are purists of structured programming who would advise against such short circuits as next, and last.

Those purists probably don't really understand what structured programming is really about and want to make it a bureaucratic straight jacket.

I remember that one of my CS professors once chastised me for having written a function or procedure (I can't remember which language it was, most probably either C or Pascal) in which I was returning early out of the function if a certain condition was met (or not met, whatever), saying me that this was a form of hidden goto (he had introduced Dijkstra's famous "Goto considered harmful" piece just a week or two before). I still received a fairly good mark, but I really felt at the time it was a ridiculous point.

I was quite happy, years later, to read somewhere in the Camel Book that programming is often building a decision tree and that it makes sense to discard early special cases, rather than having a deeply nested if/then/elsif/else conditional. Quite often, using next or last can also improve significantly the performance and the readability.

I would strongly suspect that Perl is not the right language for such purists. Let them write ten times more code-lines with A*a or Ja*a. (OK, sorry, I don't want to introduce a language flame war, but I replaced yesterday a 1500++ lines shell script with less than 20 lines Perl script, and it works better).

Replies are listed 'Best First'.
Re^3: regex 1st match only
by BillKSmith (Monsignor) on Oct 28, 2014 at 21:49 UTC
    Our objective usually is to develop correct code at a minimum lifecycle cost. Structured programming is a great tool. It should never be allowed to interfere with the objective.
    Bill
      ++, Bill.