Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.6 KiB

  1. package constant;
  2. $VERSION = '1.00';
  3. =head1 NAME
  4. constant - Perl pragma to declare constants
  5. =head1 SYNOPSIS
  6. use constant BUFFER_SIZE => 4096;
  7. use constant ONE_YEAR => 365.2425 * 24 * 60 * 60;
  8. use constant PI => 4 * atan2 1, 1;
  9. use constant DEBUGGING => 0;
  10. use constant ORACLE => '[email protected]';
  11. use constant USERNAME => scalar getpwuid($<);
  12. use constant USERINFO => getpwuid($<);
  13. sub deg2rad { PI * $_[0] / 180 }
  14. print "This line does nothing" unless DEBUGGING;
  15. =head1 DESCRIPTION
  16. This will declare a symbol to be a constant with the given scalar
  17. or list value.
  18. When you declare a constant such as C<PI> using the method shown
  19. above, each machine your script runs upon can have as many digits
  20. of accuracy as it can use. Also, your program will be easier to
  21. read, more likely to be maintained (and maintained correctly), and
  22. far less likely to send a space probe to the wrong planet because
  23. nobody noticed the one equation in which you wrote C<3.14195>.
  24. =head1 NOTES
  25. The value or values are evaluated in a list context. You may override
  26. this with C<scalar> as shown above.
  27. These constants do not directly interpolate into double-quotish
  28. strings, although you may do so indirectly. (See L<perlref> for
  29. details about how this works.)
  30. print "The value of PI is @{[ PI ]}.\n";
  31. List constants are returned as lists, not as arrays.
  32. $homedir = USERINFO[7]; # WRONG
  33. $homedir = (USERINFO)[7]; # Right
  34. The use of all caps for constant names is merely a convention,
  35. although it is recommended in order to make constants stand out
  36. and to help avoid collisions with other barewords, keywords, and
  37. subroutine names. Constant names must begin with a letter.
  38. Constant symbols are package scoped (rather than block scoped, as
  39. C<use strict> is). That is, you can refer to a constant from package
  40. Other as C<Other::CONST>.
  41. As with all C<use> directives, defining a constant happens at
  42. compile time. Thus, it's probably not correct to put a constant
  43. declaration inside of a conditional statement (like C<if ($foo)
  44. { use constant ... }>).
  45. Omitting the value for a symbol gives it the value of C<undef> in
  46. a scalar context or the empty list, C<()>, in a list context. This
  47. isn't so nice as it may sound, though, because in this case you
  48. must either quote the symbol name, or use a big arrow, (C<=E<gt>>),
  49. with nothing to point to. It is probably best to declare these
  50. explicitly.
  51. use constant UNICORNS => ();
  52. use constant LOGFILE => undef;
  53. The result from evaluating a list constant in a scalar context is
  54. not documented, and is B<not> guaranteed to be any particular value
  55. in the future. In particular, you should not rely upon it being
  56. the number of elements in the list, especially since it is not
  57. B<necessarily> that value in the current implementation.
  58. Magical values, tied values, and references can be made into
  59. constants at compile time, allowing for way cool stuff like this.
  60. (These error numbers aren't totally portable, alas.)
  61. use constant E2BIG => ($! = 7);
  62. print E2BIG, "\n"; # something like "Arg list too long"
  63. print 0+E2BIG, "\n"; # "7"
  64. =head1 TECHNICAL NOTE
  65. In the current implementation, scalar constants are actually
  66. inlinable subroutines. As of version 5.004 of Perl, the appropriate
  67. scalar constant is inserted directly in place of some subroutine
  68. calls, thereby saving the overhead of a subroutine call. See
  69. L<perlsub/"Constant Functions"> for details about how and when this
  70. happens.
  71. =head1 BUGS
  72. In the current version of Perl, list constants are not inlined
  73. and some symbols may be redefined without generating a warning.
  74. It is not possible to have a subroutine or keyword with the same
  75. name as a constant. This is probably a Good Thing.
  76. Unlike constants in some languages, these cannot be overridden
  77. on the command line or via environment variables.
  78. You can get into trouble if you use constants in a context which
  79. automatically quotes barewords (as is true for any subroutine call).
  80. For example, you can't say C<$hash{CONSTANT}> because C<CONSTANT> will
  81. be interpreted as a string. Use C<$hash{CONSTANT()}> or
  82. C<$hash{+CONSTANT}> to prevent the bareword quoting mechanism from
  83. kicking in. Similarly, since the C<=E<gt>> operator quotes a bareword
  84. immediately to its left you have to say C<CONSTANT() =E<gt> 'value'>
  85. instead of C<CONSTANT =E<gt> 'value'>.
  86. =head1 AUTHOR
  87. Tom Phoenix, E<lt>F<[email protected]>E<gt>, with help from
  88. many other folks.
  89. =head1 COPYRIGHT
  90. Copyright (C) 1997, Tom Phoenix
  91. This module is free software; you can redistribute it or modify it
  92. under the same terms as Perl itself.
  93. =cut
  94. use strict;
  95. use Carp;
  96. use vars qw($VERSION);
  97. #=======================================================================
  98. # Some of this stuff didn't work in version 5.003, alas.
  99. require 5.003_96;
  100. #=======================================================================
  101. # import() - import symbols into user's namespace
  102. #
  103. # What we actually do is define a function in the caller's namespace
  104. # which returns the value. The function we create will normally
  105. # be inlined as a constant, thereby avoiding further sub calling
  106. # overhead.
  107. #=======================================================================
  108. sub import {
  109. my $class = shift;
  110. my $name = shift or return; # Ignore 'use constant;'
  111. croak qq{Can't define "$name" as constant} .
  112. qq{ (name contains invalid characters or is empty)}
  113. unless $name =~ /^[^\W_0-9]\w*$/;
  114. my $pkg = caller;
  115. {
  116. no strict 'refs';
  117. if (@_ == 1) {
  118. my $scalar = $_[0];
  119. *{"${pkg}::$name"} = sub () { $scalar };
  120. } elsif (@_) {
  121. my @list = @_;
  122. *{"${pkg}::$name"} = sub () { @list };
  123. } else {
  124. *{"${pkg}::$name"} = sub () { };
  125. }
  126. }
  127. }
  128. 1;