Carp::Ensure

Ensure a value is of the expected type
Download

Carp::Ensure Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Stefan Merten
  • Publisher web site:
  • http://www.merten-home.de/

Carp::Ensure Tags


Carp::Ensure Description

Ensure a value is of the expected type Most of the time it's a nice feature, that Perl has no really strict type checking as in C++. However, sometimes you want to ensure, that you subs actually get the type of arguments they expect. Or they return what you expect.That is where the Carp::Ensure module may be useful. You can check every value whether it has the type you expect. You may fine tune the type checking from very coarse checking like defined vs. undefined to very detailed checks which check even the keys and values of a hash. In most places you may give alternative types so for instance a parameter can easily be checked to be of a certain type or undefined.There are checking routines for a few commonly used base types included and you may add your own checking routines so you can check for the types specific to your program.The types are described by a simple grammar which picks up as much as possible you already know from the Perl type system.SYNOPSIS use Carp::Ensure( qw( is_a ) ); ensure('string', "Some arbitrary string") if DEBUG; ensure('@integer', 1, 2, 3) if DEBUG; ensure('@integer', 1, 2, 3) if DEBUG; my %word2Int = ( one => 1, two => 2, three => 3 ); my @ints = values(%word2Int); my @wordsInts = ( keys(%word2Int), @ints ); ensure('@integer', @ints) if DEBUG; ensure('@word|integer', %word2Int) if DEBUG; ensure('%word=>integer', %word2Int) if DEBUG; die("Unexpected type") unless is_a('@word|integer', @wordsInts); die("Unexpected type") unless is_a('@@word|integer', @wordsInts, ); # Receives a string, a `Mail::Internet' object, a reference to a hash mapping # strings to integers sub someSub($$%) { ensure(, @_) if DEBUG; my( $string, $object, %hash ) = @_; # ... } Requirements: · Perl


Carp::Ensure Related Software