<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Module::Pluggable::Object;

use strict;
use File::Find ();
use File::Basename;
use File::Spec::Functions qw(splitdir catdir curdir catfile abs2rel);
use Carp qw(croak carp confess);
use Devel::InnerPackage;
use vars qw($VERSION);

use if $] &gt; 5.017, 'deprecate';

$VERSION = '5.1';


sub new {
    my $class = shift;
    my %opts  = @_;

    return bless \%opts, $class;

}

### Eugggh, this code smells 
### This is what happens when you keep adding patches
### *sigh*


sub plugins {
    my $self = shift;
    my @args = @_;

    # override 'require'
    $self-&gt;{'require'} = 1 if $self-&gt;{'inner'};

    my $filename   = $self-&gt;{'filename'};
    my $pkg        = $self-&gt;{'package'};

    # Get the exception params instantiated
    $self-&gt;_setup_exceptions;

    # automatically turn a scalar search path or namespace into a arrayref
    for (qw(search_path search_dirs)) {
        $self-&gt;{$_} = [ $self-&gt;{$_} ] if exists $self-&gt;{$_} &amp;&amp; !ref($self-&gt;{$_});
    }

    # default search path is '&lt;Module&gt;::&lt;Name&gt;::Plugin'
    $self-&gt;{'search_path'} ||= ["${pkg}::Plugin"]; 

    # default error handler
    $self-&gt;{'on_require_error'} ||= sub { my ($plugin, $err) = @_; carp "Couldn't require $plugin : $err"; return 0 };
    $self-&gt;{'on_instantiate_error'} ||= sub { my ($plugin, $err) = @_; carp "Couldn't instantiate $plugin: $err"; return 0 };

    # default whether to follow symlinks
    $self-&gt;{'follow_symlinks'} = 1 unless exists $self-&gt;{'follow_symlinks'};

    # check to see if we're running under test
    my @SEARCHDIR = exists $INC{"blib.pm"} &amp;&amp; defined $filename &amp;&amp; $filename =~ m!(^|/)blib/! &amp;&amp; !$self-&gt;{'force_search_all_paths'} ? grep {/blib/} @INC : @INC;

    # add any search_dir params
    unshift @SEARCHDIR, @{$self-&gt;{'search_dirs'}} if defined $self-&gt;{'search_dirs'};

    # set our @INC up to include and prefer our search_dirs if necessary
    my @tmp = @INC;
    unshift @tmp, @{$self-&gt;{'search_dirs'} || []};
    local @INC = @tmp if defined $self-&gt;{'search_dirs'};

    my @plugins = $self-&gt;search_directories(@SEARCHDIR);
    push(@plugins, $self-&gt;handle_inc_hooks($_, @SEARCHDIR)) for @{$self-&gt;{'search_path'}};
    push(@plugins, $self-&gt;handle_innerpackages($_)) for @{$self-&gt;{'search_path'}};
    
    # return blank unless we've found anything
    return () unless @plugins;

    # remove duplicates
    # probably not necessary but hey ho
    my %plugins;
    for(@plugins) {
        next unless $self-&gt;_is_legit($_);
        $plugins{$_} = 1;
    }

    # are we instantiating or requiring?
    if (defined $self-&gt;{'instantiate'}) {
        my $method = $self-&gt;{'instantiate'};
        my @objs   = ();
        foreach my $package (sort keys %plugins) {
            next unless $package-&gt;can($method);
            my $obj = eval { $package-&gt;$method(@_) };
            $self-&gt;{'on_instantiate_error'}-&gt;($package, $@) if $@;
            push @objs, $obj if $obj;           
        }
        return @objs;
    } else { 
        # no? just return the names
        my @objs= sort keys %plugins;
        return @objs;
    }
}

sub _setup_exceptions {
    my $self = shift;

    my %only;   
    my %except; 
    my $only;
    my $except;

    if (defined $self-&gt;{'only'}) {
        if (ref($self-&gt;{'only'}) eq 'ARRAY') {
            %only   = map { $_ =&gt; 1 } @{$self-&gt;{'only'}};
        } elsif (ref($self-&gt;{'only'}) eq 'Regexp') {
            $only = $self-&gt;{'only'}
        } elsif (ref($self-&gt;{'only'}) eq '') {
            $only{$self-&gt;{'only'}} = 1;
        }
    }
        

    if (defined $self-&gt;{'except'}) {
        if (ref($self-&gt;{'except'}) eq 'ARRAY') {
            %except   = map { $_ =&gt; 1 } @{$self-&gt;{'except'}};
        } elsif (ref($self-&gt;{'except'}) eq 'Regexp') {
            $except = $self-&gt;{'except'}
        } elsif (ref($self-&gt;{'except'}) eq '') {
            $except{$self-&gt;{'except'}} = 1;
        }
    }
    $self-&gt;{_exceptions}-&gt;{only_hash}   = \%only;
    $self-&gt;{_exceptions}-&gt;{only}        = $only;
    $self-&gt;{_exceptions}-&gt;{except_hash} = \%except;
    $self-&gt;{_exceptions}-&gt;{except}      = $except;
        
}

sub _is_legit {
    my $self   = shift;
    my $plugin = shift;
    my %only   = %{$self-&gt;{_exceptions}-&gt;{only_hash}||{}};
    my %except = %{$self-&gt;{_exceptions}-&gt;{except_hash}||{}};
    my $only   = $self-&gt;{_exceptions}-&gt;{only};
    my $except = $self-&gt;{_exceptions}-&gt;{except};
    my $depth  = () = split '::', $plugin, -1;

    return 0 if     (keys %only   &amp;&amp; !$only{$plugin}     );
    return 0 unless (!defined $only || $plugin =~ m!$only!     );

    return 0 if     (keys %except &amp;&amp;  $except{$plugin}   );
    return 0 if     (defined $except &amp;&amp;  $plugin =~ m!$except! );
    
    return 0 if     defined $self-&gt;{max_depth} &amp;&amp; $depth&gt;$self-&gt;{max_depth};
    return 0 if     defined $self-&gt;{min_depth} &amp;&amp; $depth&lt;$self-&gt;{min_depth};

    return 1;
}

sub search_directories {
    my $self      = shift;
    my @SEARCHDIR = @_;

    my @plugins;
    # go through our @INC
    foreach my $dir (@SEARCHDIR) {
        push @plugins, $self-&gt;search_paths($dir);
    }
    return @plugins;
}


sub search_paths {
    my $self = shift;
    my $dir  = shift;
    my @plugins;

    my $file_regex = $self-&gt;{'file_regex'} || qr/\.pm$/;


    # and each directory in our search path
    foreach my $searchpath (@{$self-&gt;{'search_path'}}) {
        # create the search directory in a cross platform goodness way
        my $sp = catdir($dir, (split /::/, $searchpath));

        # if it doesn't exist or it's not a dir then skip it
        next unless ( -e $sp &amp;&amp; -d _ ); # Use the cached stat the second time

        my @files = $self-&gt;find_files($sp);

        # foreach one we've found 
        foreach my $file (@files) {
            # untaint the file; accept .pm only
            next unless ($file) = ($file =~ /(.*$file_regex)$/); 
            # parse the file to get the name
            my ($name, $directory, $suffix) = fileparse($file, $file_regex);

            next if (!$self-&gt;{include_editor_junk} &amp;&amp; $self-&gt;_is_editor_junk($name));

            $directory = abs2rel($directory, $sp);

            # If we have a mixed-case package name, assume case has been preserved
            # correctly.  Otherwise, root through the file to locate the case-preserved
            # version of the package name.
            my @pkg_dirs = ();
            if ( $name eq lc($name) || $name eq uc($name) ) {
                my $pkg_file = catfile($sp, $directory, "$name$suffix");
                open PKGFILE, "&lt;$pkg_file" or die "search_paths: Can't open $pkg_file: $!";
                my $in_pod = 0;
                while ( my $line = &lt;PKGFILE&gt; ) {
                    $in_pod = 1 if $line =~ m/^=\w/;
                    $in_pod = 0 if $line =~ /^=cut/;
                    next if ($in_pod || $line =~ /^=cut/);  # skip pod text
                    next if $line =~ /^\s*#/;               # and comments
                    if ( $line =~ m/^\s*package\s+(.*::)?($name)\s*;/i ) {
                        @pkg_dirs = split /::/, $1 if defined $1;;
                        $name = $2;
                        last;
                    }
                }
                close PKGFILE;
            }

            # then create the class name in a cross platform way
            $directory =~ s/^[a-z]://i if($^O =~ /MSWin32|dos/);       # remove volume
            my @dirs = ();
            if ($directory) {
                ($directory) = ($directory =~ /(.*)/);
                @dirs = grep(length($_), splitdir($directory)) 
                    unless $directory eq curdir();
                for my $d (reverse @dirs) {
                    my $pkg_dir = pop @pkg_dirs; 
                    last unless defined $pkg_dir;
                    $d =~ s/\Q$pkg_dir\E/$pkg_dir/i;  # Correct case
                }
            } else {
                $directory = "";
            }
            my $plugin = join '::', $searchpath, @dirs, $name;

            next unless $plugin =~ m!(?:[a-z\d]+)[a-z\d]*!i;

            $self-&gt;handle_finding_plugin($plugin, \@plugins)
        }

        # now add stuff that may have been in package
        # NOTE we should probably use all the stuff we've been given already
        # but then we can't unload it :(
        push @plugins, $self-&gt;handle_innerpackages($searchpath);
    } # foreach $searchpath

    return @plugins;
}

sub _is_editor_junk {
    my $self = shift;
    my $name = shift;

    # Emacs (and other Unix-y editors) leave temp files ending in a
    # tilde as a backup.
    return 1 if $name =~ /~$/;
    # Emacs makes these files while a buffer is edited but not yet
    # saved.
    return 1 if $name =~ /^\.#/;
    # Vim can leave these files behind if it crashes.
    return 1 if $name =~ /\.sw[po]$/;

    return 0;
}

sub handle_finding_plugin {
    my $self    = shift;
    my $plugin  = shift;
    my $plugins = shift;
    my $no_req  = shift || 0;
    
    return unless $self-&gt;_is_legit($plugin);
    unless (defined $self-&gt;{'instantiate'} || $self-&gt;{'require'}) {
        push @$plugins, $plugin;
        return;
    } 

    $self-&gt;{before_require}-&gt;($plugin) || return if defined $self-&gt;{before_require};
    unless ($no_req) {
        my $tmp = $@;
        my $res = eval { $self-&gt;_require($plugin) };
        my $err = $@;
        $@      = $tmp;
        if ($err) {
            if (defined $self-&gt;{on_require_error}) {
                $self-&gt;{on_require_error}-&gt;($plugin, $err) || return; 
            } else {
                return;
            }
        }
    }
    $self-&gt;{after_require}-&gt;($plugin) || return if defined $self-&gt;{after_require};
    push @$plugins, $plugin;
}

sub find_files {
    my $self         = shift;
    my $search_path  = shift;
    my $file_regex   = $self-&gt;{'file_regex'} || qr/\.pm$/;


    # find all the .pm files in it
    # this isn't perfect and won't find multiple plugins per file
    #my $cwd = Cwd::getcwd;
    my @files = ();
    { # for the benefit of perl 5.6.1's Find, localize topic
        local $_;
        File::Find::find( { no_chdir =&gt; 1, 
                            follow   =&gt; $self-&gt;{'follow_symlinks'}, 
                            wanted   =&gt; sub { 
                             # Inlined from File::Find::Rule C&lt; name =&gt; '*.pm' &gt;
                             return unless $File::Find::name =~ /$file_regex/;
                             (my $path = $File::Find::name) =~ s#^\\./##;
                             push @files, $path;
                           }
                      }, $search_path );
    }
    #chdir $cwd;
    return @files;

}

sub handle_inc_hooks {
    my $self      = shift;
    my $path      = shift;
    my @SEARCHDIR = @_;

    my @plugins;
    for my $dir ( @SEARCHDIR ) {
        next unless ref $dir &amp;&amp; eval { $dir-&gt;can( 'files' ) };

        foreach my $plugin ( $dir-&gt;files ) {
            $plugin =~ s/\.pm$//;
            $plugin =~ s{/}{::}g;
            next unless $plugin =~ m!^${path}::!;
            $self-&gt;handle_finding_plugin( $plugin, \@plugins );
        }
    }
    return @plugins;
}

sub handle_innerpackages {
    my $self = shift;
    return () if (exists $self-&gt;{inner} &amp;&amp; !$self-&gt;{inner});

    my $path = shift;
    my @plugins;

    foreach my $plugin (Devel::InnerPackage::list_packages($path)) {
        $self-&gt;handle_finding_plugin($plugin, \@plugins, 1);
    }
    return @plugins;

}


sub _require {
    my $self   = shift;
    my $pack   = shift;
    eval "CORE::require $pack";
    die ($@) if $@;
    return 1;
}


1;

=pod

=head1 NAME

Module::Pluggable::Object - automatically give your module the ability to have plugins

=head1 SYNOPSIS


Simple use Module::Pluggable -

    package MyClass;
    use Module::Pluggable::Object;
    
    my $finder = Module::Pluggable::Object-&gt;new(%opts);
    print "My plugins are: ".join(", ", $finder-&gt;plugins)."\n";

=head1 DESCRIPTION

Provides a simple but, hopefully, extensible way of having 'plugins' for 
your module. Obviously this isn't going to be the be all and end all of
solutions but it works for me.

Essentially all it does is export a method into your namespace that 
looks through a search path for .pm files and turn those into class names. 

Optionally it instantiates those classes for you.

This object is wrapped by C&lt;Module::Pluggable&gt;. If you want to do something
odd or add non-general special features you're probably best to wrap this
and produce your own subclass.

=head1 OPTIONS

See the C&lt;Module::Pluggable&gt; docs.

=head1 AUTHOR

Simon Wistow &lt;simon@thegestalt.org&gt;

=head1 COPYING

Copyright, 2006 Simon Wistow

Distributed under the same terms as Perl itself.

=head1 BUGS

None known.

=head1 SEE ALSO

L&lt;Module::Pluggable&gt;

=cut 

</pre></body></html>