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

At my company the have departmentalized code... I.E. we have to talk to other department's code that we can't edit. The code that we used to use to access these was a hack job that required the file it needed on every call to the FCGI. This is terribly inefficient, so we fixed it. however...

The .pl files we were requiring are all in the main:: scope, and there are function names that collide, creating havoc. I would like to force each .pl into it's own namespace, and am not sure how (short of making a wrapper .pl file that declares a package).

package foo; require './foo.pl'; package main;
was suggested, but doesn't work right, since inside the require perl drops back to the container package, main::

I think I can fix this by making an object oriented interface, but does anyone know a way to do it with the requires? Whitout making a stub file for each?

                - Ant
                - Some of my best work - Fish Dinner

Replies are listed 'Best First'.
Re: Wrapping requires in a package...
by stefp (Vicar) on Sep 07, 2001 at 22:44 UTC
    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

      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")
Re: Wrapping requires in a package...
by perrin (Chancellor) on Sep 08, 2001 at 00:50 UTC
    I'm not positive, but I think "do foo.pl" might be what you want. I think the do() command evals code in the current package.

    However, having libraries that don't declare a unique package is terrible and you should really bitch and moan until those people fix their code.

      Sorry, I forgot to mention that in my testing, do had the same inconsistant behavior as require that I report elsewhere in this thread.

              - tye (but my friends call me "Tye")
        I don't think it matters as long as the code he's do()ing doesn't have any use of __PACKAGE__ constructs or declare "package main" anywhere. The subs will be defined in the right package.