in reply to Re: Re: Re: use/require $MODULE (a variable)
in thread use/require $MODULE (a variable)

Ah, I see, well, there are a few tricks you can do for plain .pl files... you could do (sort of) what Exporter is doing, which is symbol table munging, or you could cheat a little and do something like

## beware! very much untested, it's incomplete, ## and it needs lots of error checking. use IO::File; sub import { ## if the module is a plain .pl file... my $fh = IO::File->new( $module ); local $/ = undef; my $text = <$fh>; undef $fh; my $caller_pkg = ((caller())[0] eval "package $caller_pkg; $text"; }

Obviously, this is a hack and it won't always work (furthermore, it's ugly). But at least you could *attempt* to load them files to the correct namespace. ;)

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: use/require $MODULE (a variable)
by cadphile (Beadle) on Mar 18, 2002 at 09:08 UTC
    I'm tempted to try your suggestion, just because it's a bit arcane, but alas, a very simple solution has been found: Since no .pl file package is exporting (unless it names it's subroutines something like
    sub main'myfoo {}
    there is really no difference in the way I need to handle .pl packages, or just .pl libraries.

    In either case, what I'm doing is:

    if ( $file =~ /\.pl$/ ) { package main; #printf("(package::main): require \"$file\"\n"); eval "require \"$file\""; package PmLoader; } else { (my $module = $file) =~ s/\.pm$//; #printf("(module): require $module\n"); eval "require $module"; #printf("(module): $module->export_to_level(2);\n"); eval "$module->export_to_level(2)"; }
    This combination works properly for both types of .pl files, and for .pm modules that are Exporters. Hmmm, what if the .pm module doesn't export... I'd better think about that one.

    Thanks! and good night...
    -Cadphile...