Perl Monk, Perl Meditation | |
PerlMonks |
Re: Parameter Asserts - How do you use them?by adrianh (Chancellor) |
on Apr 16, 2003 at 21:16 UTC ( [id://251054]=note: print w/replies, xml ) | Need Help?? |
How would I code "parameter asserts"? I checked the camel book, but there doesn't seem to be an "assert" keyword. Depending on mood I may do something like this:
or use a module like Params::Validate or Carp::Assert. Do you personally use them in your code? Why or why not? Yes, I tend to use them. For two reasons:
Reasons not to use them: Efficiency. Assertions take time and in a tight loop or in a method that is called many times this can add up. You can use constants to allow you to switch your assertions on/off at compile time (the documentation for Carp::Assert has some examples of this.) There are some indications that there will be "real" assertions in the next perl5 that can be switched on/off without a compile time penalty. When it's obvious that they're not needed (for some definition of "obvious" :-). For example, in:
the two assertions are redundant (IMHO). If $self isn't a Foo object then somebody is calling my method as a subroutine and deserves all that they get. If $n isn't a number then the addition is going to throw a warning anyway.
In Section
Seekers of Perl Wisdom
|
|