Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: Thoughts on using subroutines to clean up code?

by stevieb (Canon)
on Mar 10, 2017 at 16:16 UTC ( [id://1184181]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1184181]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found