in reply to Re^2: Idiom for a regex translation
in thread Idiom for a regex translation
sub squish { my $copy = shift; $copy =~ s/\W//g; return $copy; }
It is not the action (the substitution) that warrants the subroutine, but the concept ("squish"). It is never the simplicity of the action that determines whether the concept is "too simple to warrant a subroutine of its own" - for example, it would be useful to do this if you were using squish() in several places in the code but might want to change it in the future, or add diagnostics etc.
But even if it will only ever be used once, putting a block of code into a named subroutine is a very useful technique for writing self-documenting code, by associating the relevant concept with the code through the name of the subroutine.
Hugo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Idiom for a regex translation
by reasonablekeith (Deacon) on Apr 22, 2005 at 10:20 UTC |