|Perl: the Markov chain saw|
Re: How to export multiple packages in one file to another Perl program?by Tanktalus (Canon)
|on Oct 05, 2012 at 03:34 UTC||Need Help??|
"How" really depends on what you're trying to accomplish. Generally, I try to avoid this, but it's definitely not always possible.
1. As far as I'm aware, you can't export my variables. Use our instead. The issue here is that my variables are lexical variables, and to export a variable it must be global. (Variables created with our are also lexical, but they're aliases to global variables in the package in which the our occurred. If that's confusing, as long as you only have one package per file then it's roughly the same thing as "it declares a global variable".)
2. Don't put more than one package in the same file. Really, don't. Yes, some people do it. I even do it. But if you're getting confused by it, don't. I only do it when it's a small hidden package that I don't expose to anyone. That sounds to be about the opposite of what I interpret you're trying to explain that you're doing. If that sounds like someone is confused, it's because I am.
3. You don't export packages. use STDLIB qw(OBJECT1) doesn't make any sense when OBJECT1 is a package.
4. Package names are generally TitleCase, not ALLUPPERCASE, even if IT_HAS_UNDERSCORES. Constants are often all uppercase, but not package names.
5. Only the "primary" package in a file gets to export stuff. While this is not technically true, it's close enough. When I "use STDLIB", only STDLIB's import gets called. That means either STDLIB's import needs to be super fancy, or I can only import stuff from the STDLIB package.
6. The "1;" is a file-level thing. So it's only needed once at the end of the entire file.
7. XY Problem. What are you really trying to do? What interface are you trying to provide? If it's simply that someone can say my $obj = OBJECT1->new(), then you don't need to export anything. If it's that you want them to say my $obj = OBJECT1->new($OBJ1_CONSTANT1), consider using strings instead, or multiple constructors, e.g., my $obj = OBJECT1->new_with_const1(). Ok, that last one isn't a good name, but neither is OBJ1_CONSTANT1, so I can't suggest a better name.
8. Oh, and "my OBJ1_CONSTANT1;" doesn't make any sense. You're missing the sigil, so I'm not sure what type of constant you're going for here.
9. Once you've separated everything into separate files, take a look at Import::Into. I've used this to import from module X when doing "use Y;". Works pretty good so far, though I can imagine some places where even that won't be sufficient (but we're talking about arcane stuff, and I'd encourage avoiding said arcane stuff).
Hope that helps.