in reply to how to import a module var

Unless the variable is exported (in which case you can import it with use Module qw($variable)), this is probably a bad idea, because you're accessing a module internal which can change its behaviour at any given time, thereby breaking your code. The module has an interface for a reason, use it. If the module does not give you a specific bit of information that you need, write a patch and add a method. If the module author is not willing to accept your patch, that's a pretty good indicator that there's a better way of doing what you want.


Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

Replies are listed 'Best First'.
Re^2: how to import a module var
by philcrow (Priest) on Nov 22, 2005 at 14:25 UTC
    I want to second the above. It's always dodgy to export anything from an OO module. If your module has a method called new, you need a very good reason for it to export anything. If outsiders need to access it, make a method for them to use.

    This is especially important when cross language programs come along. When Perl is tied to Java or Python, those other languages can't see the exports, they need methods to call.

    Perhaps the only valid use of exports in the OO context is for mixin modules which provide exported subs that lots of other classes can import into their packages. But those exporting mixin providers don't have constructors. Which leaves me back at my rule of thumb: constructors and Exporter don't mix.

    This is not to say that clever OO modules might not have a magical import that does all sorts of mischief (like fabricating whole classes). But they shouldn't actually put anything into the caller's name space like Exporter does.

    Phil

      I sometimes export a constructor. Why? Because sometimes, I rather write:
      my $obj = new_feeble();
      than
      my $obj = Feeble->new();
      The former requires less tokens. ;-)

      And I'll export constants from my OO modules if I feel like it.

      Perl --((8:>*