++ed by:

94 PAUSE users
66 non-PAUSE users.

Chad Granum
and 1 contributors


Test::Builder::Util - Internal tools for Test::Builder and friends


Tools for generating accessors and other object bits and pieces.


    #Imports a sub named 'new' and all the other tools.
    use Test::Builder::Util;

    # Define some exports
    export 'foo'; # Export the 'foo' sub
    export bar => sub { ... }; # export an anon sub named bar

    # Generate some accessors
    accessors qw/yabba dabba doo/;



Generic constructor method, can be used in almost any package. Takes key/value pairs as arguments. Key is assumed to be the name of a method or accessor. The method named for the key is called with the value as an argument. You can also define an 'init' method which this will call for you on the newly created object.


Importing this method lets you define exports.

$class->export_to($dest_package, @names)

Export @names to the package $dest_package


Export the subs named in @names.

export($name => sub { ... })

Export a sub named $name. Optionally a coderef may be used.

accessor($name, sub { return $DEFAULT })

Define an accessor. A default value can be specified via a coderef.

accessors(qw/sub1 sub2 .../)

Define several read/write accessors at once.

transform($name, sub { ($self, @args) = @_; ... })
transform($name, $attr, sub { ($self, @args) = @_; ... })

Define a read/write accessor that transforms whatever you assign to it via the given coderef. $attr is optional and defaults to $name. $attr is the key inside the blessed object hash used to store the field.

delta($name => $default)
deltas(qw/name1 name2 .../)
atomic_delta($name => $default)
atomic_deltas(qw/name1 name2 .../)

A delta accessor is an accessor that adds the numeric argument to the current value. Optionally a default value can be specified, otherwise 0 is used.

The atomic variations are thread-safe.

$success = try { ... }
($success, $error) = try { ... }

Eval the codeblock, return success or failure, and optionally the error message. This code protects $@ and $!, they will be restored by the end of the run. This code also temporarily blocks $SIG{DIE} handlers.

protect { ... }

Similar to try, except that it does not catch exceptions. The idea here is to protect $@ and $! from changes. $@ and $! will be restored to whatever they were before the run so long as it is successful. If the run fails $! will still be restored, but $@ will contain the exception being thrown.

$coderef = package_sub($package, $subname)

Find a sub in a package, returns the coderef if it is present, otherwise it returns undef. This is similar to $package->can($subname) except that it ignores inheritance.

$meta = is_tester($package)

Check if a package is a tester, return the metadata if it is.

$meta = is_provider($package)

Check if a package is a provider, return the metadata if it is.

$TB = find_builder()

Find the Test::Builder instance to use.


Chad Granum <exodist@cpan.org>


The source code repository for Test::More can be found at http://github.com/Test-More/test-more/.


Copyright 2014 Chad Granum <exodist7@gmail.com>.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://www.perl.com/perl/misc/Artistic.html