PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Runtime/Tests/LinqDlrTests/testenv/perl/lib/constant.pm

#
Perl | 278 lines | 210 code | 48 blank | 20 comment | 16 complexity | 79500a055536ec923655b59fd7ffd1dc MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. package constant;
  2. use strict;
  3. use 5.005_64;
  4. use warnings::register;
  5. our($VERSION, %declared);
  6. $VERSION = '1.02';
  7. #=======================================================================
  8. # Some names are evil choices.
  9. my %keywords = map +($_, 1), qw{ BEGIN INIT CHECK END DESTROY AUTOLOAD };
  10. my %forced_into_main = map +($_, 1),
  11. qw{ STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG };
  12. my %forbidden = (%keywords, %forced_into_main);
  13. #=======================================================================
  14. # import() - import symbols into user's namespace
  15. #
  16. # What we actually do is define a function in the caller's namespace
  17. # which returns the value. The function we create will normally
  18. # be inlined as a constant, thereby avoiding further sub calling
  19. # overhead.
  20. #=======================================================================
  21. sub import {
  22. my $class = shift;
  23. return unless @_; # Ignore 'use constant;'
  24. my $name = shift;
  25. unless (defined $name) {
  26. require Carp;
  27. Carp::croak("Can't use undef as constant name");
  28. }
  29. my $pkg = caller;
  30. # Normal constant name
  31. if ($name =~ /^_?[^\W_0-9]\w*\z/ and !$forbidden{$name}) {
  32. # Everything is okay
  33. # Name forced into main, but we're not in main. Fatal.
  34. } elsif ($forced_into_main{$name} and $pkg ne 'main') {
  35. require Carp;
  36. Carp::croak("Constant name '$name' is forced into main::");
  37. # Starts with double underscore. Fatal.
  38. } elsif ($name =~ /^__/) {
  39. require Carp;
  40. Carp::croak("Constant name '$name' begins with '__'");
  41. # Maybe the name is tolerable
  42. } elsif ($name =~ /^[A-Za-z_]\w*\z/) {
  43. # Then we'll warn only if you've asked for warnings
  44. if (warnings::enabled()) {
  45. if ($keywords{$name}) {
  46. warnings::warn("Constant name '$name' is a Perl keyword");
  47. } elsif ($forced_into_main{$name}) {
  48. warnings::warn("Constant name '$name' is " .
  49. "forced into package main::");
  50. } else {
  51. # Catch-all - what did I miss? If you get this error,
  52. # please let me know what your constant's name was.
  53. # Write to <rootbeer@redcat.com>. Thanks!
  54. warnings::warn("Constant name '$name' has unknown problems");
  55. }
  56. }
  57. # Looks like a boolean
  58. # use constant FRED == fred;
  59. } elsif ($name =~ /^[01]?\z/) {
  60. require Carp;
  61. if (@_) {
  62. Carp::croak("Constant name '$name' is invalid");
  63. } else {
  64. Carp::croak("Constant name looks like boolean value");
  65. }
  66. } else {
  67. # Must have bad characters
  68. require Carp;
  69. Carp::croak("Constant name '$name' has invalid characters");
  70. }
  71. {
  72. no strict 'refs';
  73. my $full_name = "${pkg}::$name";
  74. $declared{$full_name}++;
  75. if (@_ == 1) {
  76. my $scalar = $_[0];
  77. *$full_name = sub () { $scalar };
  78. } elsif (@_) {
  79. my @list = @_;
  80. *$full_name = sub () { @list };
  81. } else {
  82. *$full_name = sub () { };
  83. }
  84. }
  85. }
  86. 1;
  87. __END__
  88. =head1 NAME
  89. constant - Perl pragma to declare constants
  90. =head1 SYNOPSIS
  91. use constant BUFFER_SIZE => 4096;
  92. use constant ONE_YEAR => 365.2425 * 24 * 60 * 60;
  93. use constant PI => 4 * atan2 1, 1;
  94. use constant DEBUGGING => 0;
  95. use constant ORACLE => 'oracle@cs.indiana.edu';
  96. use constant USERNAME => scalar getpwuid($<);
  97. use constant USERINFO => getpwuid($<);
  98. sub deg2rad { PI * $_[0] / 180 }
  99. print "This line does nothing" unless DEBUGGING;
  100. # references can be constants
  101. use constant CHASH => { foo => 42 };
  102. use constant CARRAY => [ 1,2,3,4 ];
  103. use constant CPSEUDOHASH => [ { foo => 1}, 42 ];
  104. use constant CCODE => sub { "bite $_[0]\n" };
  105. print CHASH->{foo};
  106. print CARRAY->[$i];
  107. print CPSEUDOHASH->{foo};
  108. print CCODE->("me");
  109. print CHASH->[10]; # compile-time error
  110. =head1 DESCRIPTION
  111. This will declare a symbol to be a constant with the given scalar
  112. or list value.
  113. When you declare a constant such as C<PI> using the method shown
  114. above, each machine your script runs upon can have as many digits
  115. of accuracy as it can use. Also, your program will be easier to
  116. read, more likely to be maintained (and maintained correctly), and
  117. far less likely to send a space probe to the wrong planet because
  118. nobody noticed the one equation in which you wrote C<3.14195>.
  119. =head1 NOTES
  120. The value or values are evaluated in a list context. You may override
  121. this with C<scalar> as shown above.
  122. These constants do not directly interpolate into double-quotish
  123. strings, although you may do so indirectly. (See L<perlref> for
  124. details about how this works.)
  125. print "The value of PI is @{[ PI ]}.\n";
  126. List constants are returned as lists, not as arrays.
  127. $homedir = USERINFO[7]; # WRONG
  128. $homedir = (USERINFO)[7]; # Right
  129. The use of all caps for constant names is merely a convention,
  130. although it is recommended in order to make constants stand out
  131. and to help avoid collisions with other barewords, keywords, and
  132. subroutine names. Constant names must begin with a letter or
  133. underscore. Names beginning with a double underscore are reserved. Some
  134. poor choices for names will generate warnings, if warnings are enabled at
  135. compile time.
  136. Constant symbols are package scoped (rather than block scoped, as
  137. C<use strict> is). That is, you can refer to a constant from package
  138. Other as C<Other::CONST>.
  139. As with all C<use> directives, defining a constant happens at
  140. compile time. Thus, it's probably not correct to put a constant
  141. declaration inside of a conditional statement (like C<if ($foo)
  142. { use constant ... }>).
  143. Omitting the value for a symbol gives it the value of C<undef> in
  144. a scalar context or the empty list, C<()>, in a list context. This
  145. isn't so nice as it may sound, though, because in this case you
  146. must either quote the symbol name, or use a big arrow, (C<=E<gt>>),
  147. with nothing to point to. It is probably best to declare these
  148. explicitly.
  149. use constant UNICORNS => ();
  150. use constant LOGFILE => undef;
  151. The result from evaluating a list constant in a scalar context is
  152. not documented, and is B<not> guaranteed to be any particular value
  153. in the future. In particular, you should not rely upon it being
  154. the number of elements in the list, especially since it is not
  155. B<necessarily> that value in the current implementation.
  156. Magical values, tied values, and references can be made into
  157. constants at compile time, allowing for way cool stuff like this.
  158. (These error numbers aren't totally portable, alas.)
  159. use constant E2BIG => ($! = 7);
  160. print E2BIG, "\n"; # something like "Arg list too long"
  161. print 0+E2BIG, "\n"; # "7"
  162. Dereferencing constant references incorrectly (such as using an array
  163. subscript on a constant hash reference, or vice versa) will be trapped at
  164. compile time.
  165. In the rare case in which you need to discover at run time whether a
  166. particular constant has been declared via this module, you may use
  167. this function to examine the hash C<%constant::declared>. If the given
  168. constant name does not include a package name, the current package is
  169. used.
  170. sub declared ($) {
  171. use constant 1.01; # don't omit this!
  172. my $name = shift;
  173. $name =~ s/^::/main::/;
  174. my $pkg = caller;
  175. my $full_name = $name =~ /::/ ? $name : "${pkg}::$name";
  176. $constant::declared{$full_name};
  177. }
  178. =head1 TECHNICAL NOTE
  179. In the current implementation, scalar constants are actually
  180. inlinable subroutines. As of version 5.004 of Perl, the appropriate
  181. scalar constant is inserted directly in place of some subroutine
  182. calls, thereby saving the overhead of a subroutine call. See
  183. L<perlsub/"Constant Functions"> for details about how and when this
  184. happens.
  185. =head1 BUGS
  186. In the current version of Perl, list constants are not inlined
  187. and some symbols may be redefined without generating a warning.
  188. It is not possible to have a subroutine or keyword with the same
  189. name as a constant in the same package. This is probably a Good Thing.
  190. A constant with a name in the list C<STDIN STDOUT STDERR ARGV ARGVOUT
  191. ENV INC SIG> is not allowed anywhere but in package C<main::>, for
  192. technical reasons.
  193. Even though a reference may be declared as a constant, the reference may
  194. point to data which may be changed, as this code shows.
  195. use constant CARRAY => [ 1,2,3,4 ];
  196. print CARRAY->[1];
  197. CARRAY->[1] = " be changed";
  198. print CARRAY->[1];
  199. Unlike constants in some languages, these cannot be overridden
  200. on the command line or via environment variables.
  201. You can get into trouble if you use constants in a context which
  202. automatically quotes barewords (as is true for any subroutine call).
  203. For example, you can't say C<$hash{CONSTANT}> because C<CONSTANT> will
  204. be interpreted as a string. Use C<$hash{CONSTANT()}> or
  205. C<$hash{+CONSTANT}> to prevent the bareword quoting mechanism from
  206. kicking in. Similarly, since the C<=E<gt>> operator quotes a bareword
  207. immediately to its left, you have to say C<CONSTANT() =E<gt> 'value'>
  208. (or simply use a comma in place of the big arrow) instead of
  209. C<CONSTANT =E<gt> 'value'>.
  210. =head1 AUTHOR
  211. Tom Phoenix, E<lt>F<rootbeer@redcat.com>E<gt>, with help from
  212. many other folks.
  213. =head1 COPYRIGHT
  214. Copyright (C) 1997, 1999 Tom Phoenix
  215. This module is free software; you can redistribute it or modify it
  216. under the same terms as Perl itself.
  217. =cut