Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to process hundreds of multilingual text files each of which requires some specific preprocessing such as removing undesirable characters or formatting changes. During prototyping I developed a main process_document program which includes functions to filter specific files. However I don't want to have to include hundreds of subroutines. I would like to let a user specify a module and function to be called by the program.
ex: process_document -f Module::filter_function -i file_name where process_document does: # parse -f argument into $module, $function eval "require $module"; # openfile # $document set to document # do some stuff to $document #do user specified processing $document = $function($document); # do more stuff
I can't get the string $function to be executed as the filter_function. Any ideas would be appreciated.

Replies are listed 'Best First'.
Re (tilly) 1: How to specify a specific Module::subroutine as command option
by tilly (Archbishop) on Jul 25, 2001 at 03:49 UTC
    With what you look to be trying, try this:
    # Takes a package name, loads the associated module. sub load_module { my $pkg = shift; my $file = $pkg; $file =~ s/::/\//g; $file .= ".pm"; require $file; } # Then at the start of your code, assuming you already # processed the module_name into $module load_module($module); my $parser = $module->new(); # Proceed...
    If you go this approach you should understand the basics of Perl's OO support. You should definitely understand bless, have read perltoot, etc.
Re: How to specify a specific Module::subroutine as command option
by petral (Curate) on Jul 25, 2001 at 03:20 UTC
    You may just have mistyped while posting but you want:
    $function->($document); # or &$function($document);
    You might want to check out use autouse;instead, if your modules are fairly stable.  On the other hand, the loading of hundreds of subroutines may not be as bad as you think and the extra indirection through symbolic references adds its own overhead. Have you benchmarked or watched memory usage?  Also, of course, the user has to call the right module and function and spell them right (and you have to turn off strict 'refs' for the call).  Its the sort of thing that makes one think "there must be a better way".

      p