#! /usr/bin/perl -I. use strict; use warnings; use 5.010.000; use File::Basename qw( basename ); my @dispatch = ( { file => 'x5_1.pl', subroutine => 'foo', # string here, not coderef }, { file => 'x5_2.pl', subroutine => 'foo', # string here, not coderef }, ); sub foo { say "wrong sub"; } for my $ref ( @dispatch ) { # clean up file name for usage as namespace name (remove file path and extension and convert '-' to '_') ( my $namespace = basename( $ref->{file}, '.pl' ) ) =~ tr/-/_/; eval <<"EVAL_CODE"; package $namespace; # declare package to contain imported functions require "$ref->{file}"; # "import" function(s) $ref->{subroutine}('a'); # call function with string-eval; remember to insert error handling # We are inside package MyModule, so no need for package name in string EVAL_CODE warn "$@\n" if $@; } say "At the end:"; # just a check what happens now with main::foo foo( 'b' );