in reply to Re^2: Thoughts on using subroutines to clean up code?
in thread Thoughts on using subroutines to clean up code?
Often, I put parameter and other validation routines into 'private' subs (leading underscore on a sub name is not enforced, but it's widely known not to use these special functions directly). They are often fit for a single purpose, and not reused. Here's an extremely trivial example. Now, if there's a bug found where we need another check, it goes into the validation function, and doesn't pollute the main subroutine:
use warnings; use strict; sub func { my ($x, $y) = @_; _validate($x, $y); print "$x, $y\n"; } sub _validate { my ($x, $y) = @_; die if $x <= 20; die if $y !~ /^\w\d{2}/; die if length $y > 10; die if $y =~ /y/; }
This is especially handy when using an editor that folds your subs for you...
|
|---|