Leaked source code of windows server 2003
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.

399 lines
12 KiB

  1. package attributes;
  2. $VERSION = 0.03;
  3. @EXPORT_OK = qw(get reftype);
  4. @EXPORT = ();
  5. %EXPORT_TAGS = (ALL => [@EXPORT, @EXPORT_OK]);
  6. use strict;
  7. sub croak {
  8. require Carp;
  9. goto &Carp::croak;
  10. }
  11. sub carp {
  12. require Carp;
  13. goto &Carp::carp;
  14. }
  15. ## forward declaration(s) rather than wrapping the bootstrap call in BEGIN{}
  16. #sub reftype ($) ;
  17. #sub _fetch_attrs ($) ;
  18. #sub _guess_stash ($) ;
  19. #sub _modify_attrs ;
  20. #sub _warn_reserved () ;
  21. #
  22. # The extra trips through newATTRSUB in the interpreter wipe out any savings
  23. # from avoiding the BEGIN block. Just do the bootstrap now.
  24. BEGIN { bootstrap }
  25. sub import {
  26. @_ > 2 && ref $_[2] or do {
  27. require Exporter;
  28. goto &Exporter::import;
  29. };
  30. my (undef,$home_stash,$svref,@attrs) = @_;
  31. my $svtype = uc reftype($svref);
  32. my $pkgmeth;
  33. $pkgmeth = UNIVERSAL::can($home_stash, "MODIFY_${svtype}_ATTRIBUTES")
  34. if defined $home_stash && $home_stash ne '';
  35. my @badattrs;
  36. if ($pkgmeth) {
  37. my @pkgattrs = _modify_attrs($svref, @attrs);
  38. @badattrs = $pkgmeth->($home_stash, $svref, @attrs);
  39. if (!@badattrs && @pkgattrs) {
  40. return unless _warn_reserved;
  41. @pkgattrs = grep { m/\A[[:lower:]]+(?:\z|\()/ } @pkgattrs;
  42. if (@pkgattrs) {
  43. for my $attr (@pkgattrs) {
  44. $attr =~ s/\(.+\z//s;
  45. }
  46. my $s = ((@pkgattrs == 1) ? '' : 's');
  47. carp "$svtype package attribute$s " .
  48. "may clash with future reserved word$s: " .
  49. join(' : ' , @pkgattrs);
  50. }
  51. }
  52. }
  53. else {
  54. @badattrs = _modify_attrs($svref, @attrs);
  55. }
  56. if (@badattrs) {
  57. croak "Invalid $svtype attribute" .
  58. (( @badattrs == 1 ) ? '' : 's') .
  59. ": " .
  60. join(' : ', @badattrs);
  61. }
  62. }
  63. sub get ($) {
  64. @_ == 1 && ref $_[0] or
  65. croak 'Usage: '.__PACKAGE__.'::get $ref';
  66. my $svref = shift;
  67. my $svtype = uc reftype $svref;
  68. my $stash = _guess_stash $svref;
  69. $stash = caller unless defined $stash;
  70. my $pkgmeth;
  71. $pkgmeth = UNIVERSAL::can($stash, "FETCH_${svtype}_ATTRIBUTES")
  72. if defined $stash && $stash ne '';
  73. return $pkgmeth ?
  74. (_fetch_attrs($svref), $pkgmeth->($stash, $svref)) :
  75. (_fetch_attrs($svref))
  76. ;
  77. }
  78. sub require_version { goto &UNIVERSAL::VERSION }
  79. 1;
  80. __END__
  81. #The POD goes here
  82. =head1 NAME
  83. attributes - get/set subroutine or variable attributes
  84. =head1 SYNOPSIS
  85. sub foo : method ;
  86. my ($x,@y,%z) : Bent ;
  87. my $s = sub : method { ... };
  88. use attributes (); # optional, to get subroutine declarations
  89. my @attrlist = attributes::get(\&foo);
  90. use attributes 'get'; # import the attributes::get subroutine
  91. my @attrlist = get \&foo;
  92. =head1 DESCRIPTION
  93. Subroutine declarations and definitions may optionally have attribute lists
  94. associated with them. (Variable C<my> declarations also may, but see the
  95. warning below.) Perl handles these declarations by passing some information
  96. about the call site and the thing being declared along with the attribute
  97. list to this module. In particular, the first example above is equivalent to
  98. the following:
  99. use attributes __PACKAGE__, \&foo, 'method';
  100. The second example in the synopsis does something equivalent to this:
  101. use attributes __PACKAGE__, \$x, 'Bent';
  102. use attributes __PACKAGE__, \@y, 'Bent';
  103. use attributes __PACKAGE__, \%z, 'Bent';
  104. Yes, that's three invocations.
  105. B<WARNING>: attribute declarations for variables are an I<experimental>
  106. feature. The semantics of such declarations could change or be removed
  107. in future versions. They are present for purposes of experimentation
  108. with what the semantics ought to be. Do not rely on the current
  109. implementation of this feature.
  110. There are only a few attributes currently handled by Perl itself (or
  111. directly by this module, depending on how you look at it.) However,
  112. package-specific attributes are allowed by an extension mechanism.
  113. (See L<"Package-specific Attribute Handling"> below.)
  114. The setting of attributes happens at compile time. An attempt to set
  115. an unrecognized attribute is a fatal error. (The error is trappable, but
  116. it still stops the compilation within that C<eval>.) Setting an attribute
  117. with a name that's all lowercase letters that's not a built-in attribute
  118. (such as "foo")
  119. will result in a warning with B<-w> or C<use warnings 'reserved'>.
  120. =head2 Built-in Attributes
  121. The following are the built-in attributes for subroutines:
  122. =over 4
  123. =item locked
  124. Setting this attribute is only meaningful when the subroutine or
  125. method is to be called by multiple threads. When set on a method
  126. subroutine (i.e., one marked with the B<method> attribute below),
  127. Perl ensures that any invocation of it implicitly locks its first
  128. argument before execution. When set on a non-method subroutine,
  129. Perl ensures that a lock is taken on the subroutine itself before
  130. execution. The semantics of the lock are exactly those of one
  131. explicitly taken with the C<lock> operator immediately after the
  132. subroutine is entered.
  133. =item method
  134. Indicates that the referenced subroutine is a method.
  135. This has a meaning when taken together with the B<locked> attribute,
  136. as described there. It also means that a subroutine so marked
  137. will not trigger the "Ambiguous call resolved as CORE::%s" warning.
  138. =item lvalue
  139. Indicates that the referenced subroutine is a valid lvalue and can
  140. be assigned to. The subroutine must return a modifiable value such
  141. as a scalar variable, as described in L<perlsub>.
  142. =back
  143. There are no built-in attributes for anything other than subroutines.
  144. =head2 Available Subroutines
  145. The following subroutines are available for general use once this module
  146. has been loaded:
  147. =over 4
  148. =item get
  149. This routine expects a single parameter--a reference to a
  150. subroutine or variable. It returns a list of attributes, which may be
  151. empty. If passed invalid arguments, it uses die() (via L<Carp::croak|Carp>)
  152. to raise a fatal exception. If it can find an appropriate package name
  153. for a class method lookup, it will include the results from a
  154. C<FETCH_I<type>_ATTRIBUTES> call in its return list, as described in
  155. L<"Package-specific Attribute Handling"> below.
  156. Otherwise, only L<built-in attributes|"Built-in Attributes"> will be returned.
  157. =item reftype
  158. This routine expects a single parameter--a reference to a subroutine or
  159. variable. It returns the built-in type of the referenced variable,
  160. ignoring any package into which it might have been blessed.
  161. This can be useful for determining the I<type> value which forms part of
  162. the method names described in L<"Package-specific Attribute Handling"> below.
  163. =back
  164. Note that these routines are I<not> exported by default.
  165. =head2 Package-specific Attribute Handling
  166. B<WARNING>: the mechanisms described here are still experimental. Do not
  167. rely on the current implementation. In particular, there is no provision
  168. for applying package attributes to 'cloned' copies of subroutines used as
  169. closures. (See L<perlref/"Making References"> for information on closures.)
  170. Package-specific attribute handling may change incompatibly in a future
  171. release.
  172. When an attribute list is present in a declaration, a check is made to see
  173. whether an attribute 'modify' handler is present in the appropriate package
  174. (or its @ISA inheritance tree). Similarly, when C<attributes::get> is
  175. called on a valid reference, a check is made for an appropriate attribute
  176. 'fetch' handler. See L<"EXAMPLES"> to see how the "appropriate package"
  177. determination works.
  178. The handler names are based on the underlying type of the variable being
  179. declared or of the reference passed. Because these attributes are
  180. associated with subroutine or variable declarations, this deliberately
  181. ignores any possibility of being blessed into some package. Thus, a
  182. subroutine declaration uses "CODE" as its I<type>, and even a blessed
  183. hash reference uses "HASH" as its I<type>.
  184. The class methods invoked for modifying and fetching are these:
  185. =over 4
  186. =item FETCH_I<type>_ATTRIBUTES
  187. This method receives a single argument, which is a reference to the
  188. variable or subroutine for which package-defined attributes are desired.
  189. The expected return value is a list of associated attributes.
  190. This list may be empty.
  191. =item MODIFY_I<type>_ATTRIBUTES
  192. This method is called with two fixed arguments, followed by the list of
  193. attributes from the relevant declaration. The two fixed arguments are
  194. the relevant package name and a reference to the declared subroutine or
  195. variable. The expected return value as a list of attributes which were
  196. not recognized by this handler. Note that this allows for a derived class
  197. to delegate a call to its base class, and then only examine the attributes
  198. which the base class didn't already handle for it.
  199. The call to this method is currently made I<during> the processing of the
  200. declaration. In particular, this means that a subroutine reference will
  201. probably be for an undefined subroutine, even if this declaration is
  202. actually part of the definition.
  203. =back
  204. Calling C<attributes::get()> from within the scope of a null package
  205. declaration C<package ;> for an unblessed variable reference will
  206. not provide any starting package name for the 'fetch' method lookup.
  207. Thus, this circumstance will not result in a method call for package-defined
  208. attributes. A named subroutine knows to which symbol table entry it belongs
  209. (or originally belonged), and it will use the corresponding package.
  210. An anonymous subroutine knows the package name into which it was compiled
  211. (unless it was also compiled with a null package declaration), and so it
  212. will use that package name.
  213. =head2 Syntax of Attribute Lists
  214. An attribute list is a sequence of attribute specifications, separated by
  215. whitespace or a colon (with optional whitespace).
  216. Each attribute specification is a simple
  217. name, optionally followed by a parenthesised parameter list.
  218. If such a parameter list is present, it is scanned past as for the rules
  219. for the C<q()> operator. (See L<perlop/"Quote and Quote-like Operators">.)
  220. The parameter list is passed as it was found, however, and not as per C<q()>.
  221. Some examples of syntactically valid attribute lists:
  222. switch(10,foo(7,3)) : expensive
  223. Ugly('\(") :Bad
  224. _5x5
  225. locked method
  226. Some examples of syntactically invalid attribute lists (with annotation):
  227. switch(10,foo() # ()-string not balanced
  228. Ugly('(') # ()-string not balanced
  229. 5x5 # "5x5" not a valid identifier
  230. Y2::north # "Y2::north" not a simple identifier
  231. foo + bar # "+" neither a colon nor whitespace
  232. =head1 EXPORTS
  233. =head2 Default exports
  234. None.
  235. =head2 Available exports
  236. The routines C<get> and C<reftype> are exportable.
  237. =head2 Export tags defined
  238. The C<:ALL> tag will get all of the above exports.
  239. =head1 EXAMPLES
  240. Here are some samples of syntactically valid declarations, with annotation
  241. as to how they resolve internally into C<use attributes> invocations by
  242. perl. These examples are primarily useful to see how the "appropriate
  243. package" is found for the possible method lookups for package-defined
  244. attributes.
  245. =over 4
  246. =item 1.
  247. Code:
  248. package Canine;
  249. package Dog;
  250. my Canine $spot : Watchful ;
  251. Effect:
  252. use attributes Canine => \$spot, "Watchful";
  253. =item 2.
  254. Code:
  255. package Felis;
  256. my $cat : Nervous;
  257. Effect:
  258. use attributes Felis => \$cat, "Nervous";
  259. =item 3.
  260. Code:
  261. package X;
  262. sub foo : locked ;
  263. Effect:
  264. use attributes X => \&foo, "locked";
  265. =item 4.
  266. Code:
  267. package X;
  268. sub Y::x : locked { 1 }
  269. Effect:
  270. use attributes Y => \&Y::x, "locked";
  271. =item 5.
  272. Code:
  273. package X;
  274. sub foo { 1 }
  275. package Y;
  276. BEGIN { *bar = \&X::foo; }
  277. package Z;
  278. sub Y::bar : locked ;
  279. Effect:
  280. use attributes X => \&X::foo, "locked";
  281. =back
  282. This last example is purely for purposes of completeness. You should not
  283. be trying to mess with the attributes of something in a package that's
  284. not your own.
  285. =head1 SEE ALSO
  286. L<perlsub/"Private Variables via my()"> and
  287. L<perlsub/"Subroutine Attributes"> for details on the basic declarations;
  288. L<attrs> for the obsolescent form of subroutine attribute specification
  289. which this module replaces;
  290. L<perlfunc/use> for details on the normal invocation mechanism.
  291. =cut