Scalar::Util::Instance - Generates and installs is-a predicates


Scalar-Util-Instance documentation Contained in the Scalar-Util-Instance distribution.

Index


Code Index:

NAME

Top

Scalar::Util::Instance - Generates and installs is-a predicates

VERSION

Top

This document describes Scalar::Util::Instance version 0.001.

SYNOPSIS

Top

    use Scalar::Util::Instance
        { for => 'Foo', as => 'is_a_Foo' },
        { for => 'Bar', as => 'is_a_Bar' },
    ;

    # ...
    if(is_a_Foo($_)){
        # ...
    }
    elsif(is_a_Bar($_)){
        # ...
    }

DESCRIPTION

Top

Scalar::Util::Instance provides is-a predicates to look up an is-a hierarchy for specific classes. This is an alternative to blessed($obj) && $obj->isa(...), but is significantly faster than it.

INTERFACE

Top

Utility functions

Scalar::Util::Instance->generate_for(ClassName, ?PredicateName)

Generates an is-a predicate function for ClassName.

If PredicateName is specified, the method installs the generated function as that name. Otherwise returns an anonymous CODE reference.

An is-a predicate is a function which is the same as the following:

    sub is_a_some_class {
        my($obj) = @_;
        return Scalar::Util::blessed($obj) && $obj->isa($ClassName);
    }

DEPENDENCIES

Top

Perl 5.8.1 or later, and a C compiler.

BUGS

Top

No bugs have been reported.

Please report any bugs or feature requests to the author.

SEE ALSO

Top

Scalar::Util

Data::Util

AUTHOR

Top

Goro Fuji (gfx) <gfuji(at)cpan.org>

LICENSE AND COPYRIGHT

Top


Scalar-Util-Instance documentation Contained in the Scalar-Util-Instance distribution.

package Scalar::Util::Instance;

use 5.008_001;
use strict;
use warnings;

our $VERSION = '0.001';

use XSLoader;
XSLoader::load(__PACKAGE__, $VERSION);

sub import {
    my $class  = shift;

    my $into = caller;
    foreach my $config(@_){
        my $as = $config->{as};
        if(!defined $as){
            require Carp;
            Carp::croak("You must define a predicate name by 'as'");
        }
        if($as !~ /::/){
            $as = $into . '::' . $as;
        }
        $class->generate_for($config->{for}, $as);
    }
    return;
}

1;
__END__