Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: How to export multiple packages in one file to another Perl program?

by Tanktalus (Canon)
on Oct 05, 2012 at 03:34 UTC ( #997368=note: print w/replies, xml ) Need Help??

in reply to How to export multiple packages in one file to another Perl program?

"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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997368]
[tye]: Is it just me, or do most python things have terrible logging/errors?
[LanX]: but but but ... the errors are nicely formatted!
[tye]: 1) The python stack traces show you the names of variables, not the values of variables. Almost never informative.
[LanX]: do you have a py job now?
[tye]: Right now I have errors trying to fetch stuff from graphite. It says 'apps not ready, check startup logs'. I eventually find the startup logs and see 'no apps found'. Looking for fixes to this problem...
[LanX]: no idea ... ask py monks! ;-p
[tye]: it turns out that this can be caused by almost anything going wrong. If you want to know what caused the problem, you have to try to load each app by-hand to see what problems it is having, because none of those will be logged.
[tye]: No, I write more Perl than py at work. But I have to deal with plenty of py things.
[tye]: py monks would just be offended.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-09-21 20:09 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (252 votes). Check out past polls.