On Tue, Jan 10, 2006 at 08:11:43AM -0800, Matisse Enzer wrote:

> I'd like to create a class that provides a bunch of assertion  
> methods, like Carp::Assert, etc. I want to have an object oriented  
> interface, so in some code I'm developing I would have:
> 
> 
>   use Devel::Assert;
>   my $tester = Devel::Assert->new( on_fail => carp ); # or   on_fail  
> => cluck, etc.
> 
>   my $data_structure = blah_blah();
>   $tester->save($data_structure);   # Saves a clone of the data in a  
> unique slot
>   #
>   #  do stuff with $data_structure
>   #
>   $tester->has_changed($data_structure);
> 
> The trick I want is that if my code is running in a production  
> environment (perhaps determined at compile-time) then I want my  
> Devel::Assert stuff to basically disappear. So the question is, what  
> is the lowest-impact way to do that?
> 
> One easy, but probably not best way is to like this:
> 
>   sub has_changed {
>      return if is_production();
>      # do actual stuff
>    }
> 
> I'll note here that Carp::Assert handles this issue by having you  
> make all the test calls conditional:
> 
>    ASSERT( $a == $b) if DEBUG;

This isn't an answer to your question, but in general production is the
environment in which your code will be exposed to the data and
conditions which have had the least testing, and to which you will have
the least access and freedom to change things.  This is precisely the
environment in which I like to have checks to discover problems as early
as possible and to provide the most information about them.

Should you have considered this found that the checks are just too
expensive, for example, one possibility which is slightly better than
the one you proposed might be to to replace all the methods in
Devel::Assert with empty subs.  This will mean that you will still have
the overhead of a method call, which is (un)reasonably expensive in
Perl, so to eliminate the call completely you are left with a solution
such as the one you noted, where the call is conditional on some
constant which can be set to false allowing the optimiser to remove the
whole thing from the op tree.

I suppose you could consider a source filter, but I couldn't recommend
that.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

Reply via email to