> The following was supposedly scribed by
> Adrian Howard
> on Monday 26 January 2004 05:41 am:

>> The goal is to have any module under CAD::Drawing::IO::* be able to
>> act as a
>> plugin regardless of whether it was installed before or after
>> CAD::Drawing::IO.
>
>[snip]
>
>I've not used it myself, but you might want to take a look at Dave
>Rolsky's Class::Factory::Util which looks like it will do some of this
>for you.

Thanks, but that really only provides a method for finding the submodules, and 
doesn't give anything wrt how to load and call into them.  That step is 
really just a trivial bit of book-keeping overhead.

I'm more concerned with the fact that I have to use "no strict 'refs'" in 
order to call the backend functions with <package name>::save() and that my 
only other option seems to be using a dummy object (which would require every 
backend function to start with "my $useless = shift;")

Right now, both of these seem like a kludge, but anyway here is working code:

sub diskaction {
    my $self = shift;
    my ($action, $filename, $type, $opt) = @_;
    my %opts;
    (ref($opt) eq "HASH") && (%opts = %$opt);
    ($action =~ m/save|load/) ||
            croak("Cannot access disk with action:  $action\n");
    my %handlers = %CAD::Drawing::IO::handlers;
    my %check = %CAD::Drawing::IO::check_type;
    foreach my $mod (keys(%{$handlers{$action}})) {
        no strict 'refs';
        my $real_type = $check{$mod}($filename, $type);
        # check function must return true for plug-in to work on this type
        $real_type || next;
        my $call = $handlers{$action}{$mod};
        return($call->($self, $filename, {%opts, type => $real_type}));
    }
    croak("could not save $filename as $type");
}

--Eric

Reply via email to