in reply to Re: Constants and Subclasses...
in thread Constants and Subclasses...

BEGIN is only necessary because you're doing this all in a single file and need to hack the order of execution. This wouldn't normally be an issue in real code. Also, if you switched to use Parent; @ISA = 'Parent' instead of base.pm then you wouldn't need to separately call import. Further... making the parent export it's constants means that all other code that merely uses Parent will also get the constants exported and that's likely *not* what you want.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^3: Constants and Subclasses...
by ferreira (Chaplain) on Dec 19, 2006 at 11:45 UTC

    I understand this more fully now after sleeping over it and after your excellent explanation and alternative with use Parent; @ISA = 'Parent'. (But — in the single file case — realize I cannot use because that will call require which will try to read Parent from a file. In turn, use base apparently doesn't do this if it notices the package is already there — not relying only on %INC.)

    With respect to the unwanted exportation of the constants, it might be better to implement this as suggested by jettero (Re^2: Constants and Subclasses...) and do:

    our %EXPORT_TAGS = ( CONSTANTS => [qw(FOO)], # and other constants ); our @EXPORT_OK; Exporter::export_ok_tags('CONSTANTS'); # populate the @EXPORT_OK

    and then

    BEGIN { P->import(':CONSTANTS'); }

    As the constants are not exported by default anymore, the user of the code should explicitly call for them or not.

    Yeah, jettero, you were right from the beginning about the convenience of @EXPORT_OK and %EXPORT_TAGS.

      No... the most correct way to do this would be to use the normal class method syntax. This is OO so it's entirely likely that someone's going to want to subclass that constant at some point. Your static exporting circumvents inheritance.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊