2

As I have been refactoring some of my code from what someone called "perl 4 coding style" to use packages, I have a question on something I still don't quite fully understand. I know that use includes a file at compile time, and require at runtime (among other differences), both being run as if in the BEGIN section. I know that do is kind of like eval (with some differences).

I have a script (B) that can be run on its own, and I am calling it from another script (C) as a require. Script B calls a require on a .pm file (A) which has just two subs (one to set a variable, and another to return it) and a variable declared as our (I'm guessing my would achieve the same here though). This .pm is not encapsulated with package (hence the situation). If I run script B, it works without a hitch. If I require script B from script C (which also has a require for a package'd .pm that requires A.pm, and uses the accessor func), I get:

Undefined subroutine &main::func_in_A called at script_B

However, if I do A.pm in script_B everything works as expected.

As I was typing up the question, I think I realize what is happening, but let me ask anyway just to make sure.

Is this as a result of the fact that require keeps track of the files it has loaded (while do does not), such that it loads a file exactly once, despite the fact that that once was in the package's namespace, and hence the symbols will not be brought into MAIN when doing a require there? If not, then what am I missing here?

2
  • 1
    Doesn't answer your question -- hobbs already has -- but you might be interested in this Commented Sep 12, 2014 at 3:48
  • ah interesting, thanks for the link! Commented Sep 12, 2014 at 7:48

1 Answer 1

4

Yes, I think that your diagnosis is right. Since A.pm lacks a package statement, subs within it will be defined under whatever package was active when the file was evaluated. Since do has no protection against double-evaluation, A's subs up in two different packages. Since require does, they end up in one package, and which package depends on who loads A first.

Solution: don't use require (and certainly not use) on files without package statements :)

Sign up to request clarification or add additional context in comments.

3 Comments

great! Thanks for the input! I'm going to wait and see if anyone else has something to add to that.
It seems I never accepted this as the answer back when I asked it. Sorry about that!
@insaner ha, no problem at all :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.