diff options
author | Barrie Slaymaker <barries@slaysys.com> | 2001-11-28 09:10:01 -0500 |
---|---|---|
committer | Jarkko Hietaniemi <jhi@iki.fi> | 2001-11-29 01:23:08 +0000 |
commit | 5fea0f12373ae6f6aa140f470ee71f3ef680c14b (patch) | |
tree | 3aec1af768637135a03ca67345e0d8fa2315ad6e /lib/Exporter.pm | |
parent | 11a7ac70a867da65019c2c04f8677bdcf4c9693e (diff) | |
download | perl-5fea0f12373ae6f6aa140f470ee71f3ef680c14b.tar.gz |
Explain dangers & workaround for AUTOLOADed constant subs
Message-ID: <20011128140957.D5236@sizzle.whoville.com>
p4raw-id: //depot/perl@13347
Diffstat (limited to 'lib/Exporter.pm')
-rw-r--r-- | lib/Exporter.pm | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/Exporter.pm b/lib/Exporter.pm index 9a361a73fa..7067464315 100644 --- a/lib/Exporter.pm +++ b/lib/Exporter.pm @@ -307,4 +307,34 @@ unchanged but will trigger a warning (with C<-w>) to avoid misspelt tags names being silently added to @EXPORT or @EXPORT_OK. Future versions may make this a fatal error. +=head2 C<AUTOLOAD>ed Constants + +Many modules make use of C<AUTOLOAD>ing for constant subroutines to avoid +having to compile and waste memory on rarely used values (see L<perlsub> for +details on constant subroutines). Calls to such constant subroutines are not +optimized away at compile time because they can't be checked at compile time +for constancy. + +Even if a prototype is available at compile time, the body of the subroutine is +not (it hasn't been C<AUTOLOAD>ed yet). perl needs to examine both the C<()> +prototype and the body of a subroutine at compile time to detect that it can +safely replace calls to that subroutine with the constant value. + +A workaround for this is to call the constants once in a C<BEGIN> block: + + package My ; + + use Socket ; + + foo( SO_LINGER ); ## SO_LINGER NOT optimized away; called at runtime + BEGIN { SO_LINGER } + foo( SO_LINGER ); ## SO_LINGER optimized away at compile time. + +This forces the C<AUTOLOAD> for C<SOLINGER> to take place before SO_LINGER is +encountered later in C<My> package. + +If you are writing a package that C<AUTOLOAD>s, consider forcing an C<AUTOLOAD> +for any constants explicitly imported by other packages or which are usually +used when your package is C<use>d. + =cut |