package Text::Wigwam::Directives;

     # This is the namespace into which modules are loaded by default.

    use strict;
    use warnings;
    our $VERSION = '0.01';

    use Text::Wigwam::Const( qw/:all/ );
    use File::Spec::Functions( qw/catfile/ );

    sub load_up {
         # Evaluates Perl libraries within Text::Wigwam::Directives.
        no strict 'refs';
        local $@;
        my ($self, $pi_cache, $options, $Api, $finder, @files) = @_;
        return (
            "Unable to load " .
            join( ', ' => @files ) .
            " - no paths are defined for " .
            $finder->name
        ) unless $finder->get;
        foreach my $plugin ( @files ){
            next unless length $plugin;
            my $file = $finder->file($plugin, qw/ .pm .wwm .wgm /)
              or return (
               "Unable to locate $plugin in " .
               $finder->name . " path " .
               join( ", " => $finder->get )
              )
            ;
        ###    delete $INC{$file}; delete $pi_cache->{$file};
            my $code = $pi_cache->{$file} ||= eval{require $file};
            if( $@ ){
#                warn $@;
                delete $pi_cache->{$file};
                return $@
            }
            %$options = ( %$options, $code->($Api) )
              if ref($code) eq 'CODE';
        }
        return;
    }

1;
