in reply to Wrapping requires in a package...

This should do what you want. I don't think there is much prettier way out. Note that when you get back from the eval, you are in the package you started with. In other words, the package foo is scoped within the eval.

my $evalstr; { local (*F, $/); undef $/; open F, './foo.pl'; $evalstr = 'package foo; ' . <F>; } eval $evalstr or die $@;
update cuz the discussion on the CB shows I did not get my point thru

try: perl -e 'package foo; require "foo.pl"' with the content of foo.pl being simply print __PACKAGE__ and you will see that "main" is printed, not "foo"

-- stefp

Replies are listed 'Best First'.
(tye)Re: Wrapping requires in a package...
by tye (Sage) on Sep 08, 2001 at 00:42 UTC

    To expand on the update a bit, Perl has what feels like a bug to me in that if you do:

    package this; require 'that.pl';
    any subroutines defined in 'that.pl' will be placed into the a 'this' package, but __PACKAGE__ gets expanded to "main" in such code. So, in some aspects, the code is compiled in the requested package and in other aspects the code is compiled in the package "main". This probably shouldn't be possible.

    However, requiring files that don't declare what package they are in is a bad practice so I find little motivation to report this problem compared to more serious problems that I should be working on...

            - tye (but my friends call me "Tye")