in reply to Re: Thoughts on using subroutines to clean up code?
in thread Thoughts on using subroutines to clean up code?

Thanks, this helps. For some reason, I have it my head that I should only create a subroutine if it will be used more than once. Knowing that that isn't a hard and fast rule helps.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

  • Comment on Re^2: Thoughts on using subroutines to clean up code?

Replies are listed 'Best First'.
Re^3: Thoughts on using subroutines to clean up code?
by stevieb (Canon) on Mar 10, 2017 at 16:16 UTC

    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...