Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

validating sub arguments

by agianni (Hermit)
on Mar 02, 2007 at 16:19 UTC ( #602898=perlquestion: print w/replies, xml ) Need Help??
agianni has asked for the wisdom of the Perl Monks concerning the following question:

I'm getting tired of writing the code to check for subroutine/method arguments. I know there are several modules out there Arguments, GetArgs::Long, Params::Smart, etc.), but I'm not sure what works well. I'd love to hear from my fellow monks on the relative merits of the argument processing/validating modules out there.

Replies are listed 'Best First'.
Re: validating sub arguments
by Fletch (Chancellor) on Mar 02, 2007 at 16:32 UTC

    Don't forget Params::Validate (which I'm only familiar with because Mason uses it; which I guess is somewhat of an endorsement :)

Re: validating sub arguments
by Herkum (Parson) on Mar 02, 2007 at 17:23 UTC

    I guess the question you should be asking, why is unsafe data getting passed to subroutines/methods in the first place.

    For example you are writing a web page, you need to validate the data when 'Submit' the form's data. All data errors should have been caught at the point of submission, not at the point you are passing data into your routines or methods. After that, you initialize your environment to process the data.

    It is best to have one really strong layer for validating data and catch exceptions in modules with diagnostic code. Avoid having multiple layers of validations in your programs; you end will end up having multiple layers for error handling and having to decide whether an error is because of data submitted or because there was a fatal exception in that subroutine/module.

    As for validating form data, I use DataForm::Validator, it can be tough to learn at first as the documentation is not so great, but it really is very flexible.

      I guess the question you should be asking, why is unsafe data getting passed to subroutines/methods in the first place.

      Perhaps from the programmer mistyping the argument list or misunderstanding the interface?

      Validating data from external sources is always needed. Validating data from internal sources is very useful during development and testing (ala C's assert()) and sometimes during production. Params::Validate has helped save me from my own bad memory/typing a number of times. It's not too hard to set it up so you can remove it from production code easily if that's what you want.

      Actually, I'm talking more about validating the arguments passed to a given subroutine, which may or may not be in the context of a Web application. Interestingly enough, though, I'm actually rather familiar with D::FV, as we use it for validating data for our Web applications. I had never thought about using that to validate the arguments to a given sub. It seems like cutting balsa wood with a power saw, but it certainly would be an option and it would be pretty easy to build the profiles on the fly.

Re: validating sub arguments
by perrin (Chancellor) on Mar 02, 2007 at 17:15 UTC
    Another vote for Params::Validate.
Re: validating sub arguments
by dragonchild (Archbishop) on Mar 03, 2007 at 02:31 UTC

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: validating sub arguments
by adrianh (Chancellor) on Mar 04, 2007 at 10:44 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://602898]
Approved by Limbic~Region
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2018-06-23 23:20 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.