xref: /openbsd-src/gnu/usr.bin/perl/ext/attributes/attributes.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1b39c5158Smillertpackage attributes;
2b39c5158Smillert
3*3d61058aSafresh1our $VERSION = 0.36;
4b39c5158Smillert
5b39c5158Smillert@EXPORT_OK = qw(get reftype);
6b39c5158Smillert@EXPORT = ();
7b39c5158Smillert%EXPORT_TAGS = (ALL => [@EXPORT, @EXPORT_OK]);
8b39c5158Smillert
9b39c5158Smillertuse strict;
10b39c5158Smillert
11b39c5158Smillertsub croak {
12b39c5158Smillert    require Carp;
13b39c5158Smillert    goto &Carp::croak;
14b39c5158Smillert}
15b39c5158Smillert
16b39c5158Smillertsub carp {
17b39c5158Smillert    require Carp;
18b39c5158Smillert    goto &Carp::carp;
19b39c5158Smillert}
20b39c5158Smillert
219f11ffb7Safresh1# Hash of SV type (CODE, SCALAR, etc.) to regex matching deprecated
229f11ffb7Safresh1# attributes for that type.
23b39c5158Smillertmy %deprecated;
24b39c5158Smillert
25b8851fccSafresh1my %msg = (
26b8851fccSafresh1    lvalue => 'lvalue attribute applied to already-defined subroutine',
27b8851fccSafresh1   -lvalue => 'lvalue attribute removed from already-defined subroutine',
28b8851fccSafresh1    const  => 'Useless use of attribute "const"',
29b8851fccSafresh1);
30b8851fccSafresh1
31b39c5158Smillertsub _modify_attrs_and_deprecate {
32b39c5158Smillert    my $svtype = shift;
339f11ffb7Safresh1    # After we've removed a deprecated attribute from the XS code, we need to
34b39c5158Smillert    # remove it here, else it ends up in @badattrs. (If we do the deprecation in
35b39c5158Smillert    # XS, we can't control the warning based on *our* caller's lexical settings,
36b39c5158Smillert    # and the warned line is in this package)
37b39c5158Smillert    grep {
38b39c5158Smillert	$deprecated{$svtype} && /$deprecated{$svtype}/ ? do {
39b39c5158Smillert	    require warnings;
409f11ffb7Safresh1	    warnings::warnif('deprecated', "Attribute \"$1\" is deprecated, " .
419f11ffb7Safresh1                                           "and will disappear in Perl 5.28");
42b39c5158Smillert	    0;
43b8851fccSafresh1	} : $svtype eq 'CODE' && exists $msg{$_} ? do {
44898184e3Ssthen	    require warnings;
45898184e3Ssthen	    warnings::warnif(
46898184e3Ssthen		'misc',
47b8851fccSafresh1		 $msg{$_}
48898184e3Ssthen	    );
49898184e3Ssthen	    0;
50b39c5158Smillert	} : 1
51b39c5158Smillert    } _modify_attrs(@_);
52b39c5158Smillert}
53b39c5158Smillert
54b39c5158Smillertsub import {
55b39c5158Smillert    @_ > 2 && ref $_[2] or do {
56b39c5158Smillert	require Exporter;
57b39c5158Smillert	goto &Exporter::import;
58b39c5158Smillert    };
59b39c5158Smillert    my (undef,$home_stash,$svref,@attrs) = @_;
60b39c5158Smillert
61b39c5158Smillert    my $svtype = uc reftype($svref);
62b39c5158Smillert    my $pkgmeth;
63b39c5158Smillert    $pkgmeth = UNIVERSAL::can($home_stash, "MODIFY_${svtype}_ATTRIBUTES")
64b39c5158Smillert	if defined $home_stash && $home_stash ne '';
65b39c5158Smillert    my @badattrs;
66b39c5158Smillert    if ($pkgmeth) {
67b39c5158Smillert	my @pkgattrs = _modify_attrs_and_deprecate($svtype, $svref, @attrs);
68b39c5158Smillert	@badattrs = $pkgmeth->($home_stash, $svref, @pkgattrs);
69b39c5158Smillert	if (!@badattrs && @pkgattrs) {
70b39c5158Smillert            require warnings;
71b39c5158Smillert	    return unless warnings::enabled('reserved');
72b39c5158Smillert	    @pkgattrs = grep { m/\A[[:lower:]]+(?:\z|\()/ } @pkgattrs;
73b39c5158Smillert	    if (@pkgattrs) {
74b39c5158Smillert		for my $attr (@pkgattrs) {
75b39c5158Smillert		    $attr =~ s/\(.+\z//s;
76b39c5158Smillert		}
77b39c5158Smillert		my $s = ((@pkgattrs == 1) ? '' : 's');
78b39c5158Smillert		carp "$svtype package attribute$s " .
79b39c5158Smillert		    "may clash with future reserved word$s: " .
80b39c5158Smillert		    join(' : ' , @pkgattrs);
81b39c5158Smillert	    }
82b39c5158Smillert	}
83b39c5158Smillert    }
84b39c5158Smillert    else {
85b39c5158Smillert	@badattrs = _modify_attrs_and_deprecate($svtype, $svref, @attrs);
86b39c5158Smillert    }
87b39c5158Smillert    if (@badattrs) {
88b39c5158Smillert	croak "Invalid $svtype attribute" .
89b39c5158Smillert	    (( @badattrs == 1 ) ? '' : 's') .
90b39c5158Smillert	    ": " .
91b39c5158Smillert	    join(' : ', @badattrs);
92b39c5158Smillert    }
93b39c5158Smillert}
94b39c5158Smillert
95b39c5158Smillertsub get ($) {
96b39c5158Smillert    @_ == 1  && ref $_[0] or
97b39c5158Smillert	croak 'Usage: '.__PACKAGE__.'::get $ref';
98b39c5158Smillert    my $svref = shift;
99b39c5158Smillert    my $svtype = uc reftype($svref);
100b39c5158Smillert    my $stash = _guess_stash($svref);
101b39c5158Smillert    $stash = caller unless defined $stash;
102b39c5158Smillert    my $pkgmeth;
103b39c5158Smillert    $pkgmeth = UNIVERSAL::can($stash, "FETCH_${svtype}_ATTRIBUTES")
104b39c5158Smillert	if defined $stash && $stash ne '';
105b39c5158Smillert    return $pkgmeth ?
106b39c5158Smillert		(_fetch_attrs($svref), $pkgmeth->($stash, $svref)) :
107b39c5158Smillert		(_fetch_attrs($svref))
108b39c5158Smillert	;
109b39c5158Smillert}
110b39c5158Smillert
111b39c5158Smillertsub require_version { goto &UNIVERSAL::VERSION }
112b39c5158Smillert
113b39c5158Smillertrequire XSLoader;
114898184e3SsthenXSLoader::load();
115b39c5158Smillert
116b39c5158Smillert1;
117b39c5158Smillert__END__
118b39c5158Smillert#The POD goes here
119b39c5158Smillert
120b39c5158Smillert=head1 NAME
121b39c5158Smillert
122b39c5158Smillertattributes - get/set subroutine or variable attributes
123b39c5158Smillert
124b39c5158Smillert=head1 SYNOPSIS
125b39c5158Smillert
126b39c5158Smillert  sub foo : method ;
127b39c5158Smillert  my ($x,@y,%z) : Bent = 1;
128b39c5158Smillert  my $s = sub : method { ... };
129b39c5158Smillert
130b39c5158Smillert  use attributes ();	# optional, to get subroutine declarations
131b39c5158Smillert  my @attrlist = attributes::get(\&foo);
132b39c5158Smillert
133b39c5158Smillert  use attributes 'get'; # import the attributes::get subroutine
134b39c5158Smillert  my @attrlist = get \&foo;
135b39c5158Smillert
136b39c5158Smillert=head1 DESCRIPTION
137b39c5158Smillert
138b39c5158SmillertSubroutine declarations and definitions may optionally have attribute lists
139b39c5158Smillertassociated with them.  (Variable C<my> declarations also may, but see the
140b39c5158Smillertwarning below.)  Perl handles these declarations by passing some information
141b39c5158Smillertabout the call site and the thing being declared along with the attribute
142b39c5158Smillertlist to this module.  In particular, the first example above is equivalent to
143b39c5158Smillertthe following:
144b39c5158Smillert
145b39c5158Smillert    use attributes __PACKAGE__, \&foo, 'method';
146b39c5158Smillert
147b39c5158SmillertThe second example in the synopsis does something equivalent to this:
148b39c5158Smillert
149b39c5158Smillert    use attributes ();
150b39c5158Smillert    my ($x,@y,%z);
151b39c5158Smillert    attributes::->import(__PACKAGE__, \$x, 'Bent');
152b39c5158Smillert    attributes::->import(__PACKAGE__, \@y, 'Bent');
153b39c5158Smillert    attributes::->import(__PACKAGE__, \%z, 'Bent');
154b39c5158Smillert    ($x,@y,%z) = 1;
155b39c5158Smillert
156b39c5158SmillertYes, that's a lot of expansion.
157b39c5158Smillert
158b39c5158SmillertB<WARNING>: attribute declarations for variables are still evolving.
159b39c5158SmillertThe semantics and interfaces of such declarations could change in
160b39c5158Smillertfuture versions.  They are present for purposes of experimentation
161b39c5158Smillertwith what the semantics ought to be.  Do not rely on the current
162b39c5158Smillertimplementation of this feature.
163b39c5158Smillert
164b39c5158SmillertThere are only a few attributes currently handled by Perl itself (or
165b39c5158Smillertdirectly by this module, depending on how you look at it.)  However,
166b39c5158Smillertpackage-specific attributes are allowed by an extension mechanism.
167b39c5158Smillert(See L<"Package-specific Attribute Handling"> below.)
168b39c5158Smillert
169b39c5158SmillertThe setting of subroutine attributes happens at compile time.
170b39c5158SmillertVariable attributes in C<our> declarations are also applied at compile time.
171b39c5158SmillertHowever, C<my> variables get their attributes applied at run-time.
172b39c5158SmillertThis means that you have to I<reach> the run-time component of the C<my>
173b39c5158Smillertbefore those attributes will get applied.  For example:
174b39c5158Smillert
175b39c5158Smillert    my $x : Bent = 42 if 0;
176b39c5158Smillert
177b39c5158Smillertwill neither assign 42 to $x I<nor> will it apply the C<Bent> attribute
178b39c5158Smillertto the variable.
179b39c5158Smillert
180b39c5158SmillertAn attempt to set an unrecognized attribute is a fatal error.  (The
181b39c5158Smillerterror is trappable, but it still stops the compilation within that
182b39c5158SmillertC<eval>.)  Setting an attribute with a name that's all lowercase
183b39c5158Smillertletters that's not a built-in attribute (such as "foo") will result in
184b39c5158Smillerta warning with B<-w> or C<use warnings 'reserved'>.
185b39c5158Smillert
186b39c5158Smillert=head2 What C<import> does
187b39c5158Smillert
188b39c5158SmillertIn the description it is mentioned that
189b39c5158Smillert
190b39c5158Smillert  sub foo : method;
191b39c5158Smillert
192b39c5158Smillertis equivalent to
193b39c5158Smillert
194b39c5158Smillert  use attributes __PACKAGE__, \&foo, 'method';
195b39c5158Smillert
196b39c5158SmillertAs you might know this calls the C<import> function of C<attributes> at compile
197b39c5158Smillerttime with these parameters: 'attributes', the caller's package name, the reference
198b39c5158Smillertto the code and 'method'.
199b39c5158Smillert
200b39c5158Smillert  attributes->import( __PACKAGE__, \&foo, 'method' );
201b39c5158Smillert
202b39c5158SmillertSo you want to know what C<import> actually does?
203b39c5158Smillert
204b39c5158SmillertFirst of all C<import> gets the type of the third parameter ('CODE' in this case).
205b39c5158SmillertC<attributes.pm> checks if there is a subroutine called C<< MODIFY_<reftype>_ATTRIBUTES >>
206898184e3Ssthenin the caller's namespace (here: 'main').  In this case a
207898184e3Ssthensubroutine C<MODIFY_CODE_ATTRIBUTES> is required.  Then this
208898184e3Ssthenmethod is called to check if you have used a "bad attribute".
209b39c5158SmillertThe subroutine call in this example would look like
210b39c5158Smillert
211b39c5158Smillert  MODIFY_CODE_ATTRIBUTES( 'main', \&foo, 'method' );
212b39c5158Smillert
213b39c5158SmillertC<< MODIFY_<reftype>_ATTRIBUTES >> has to return a list of all "bad attributes".
214b39c5158SmillertIf there are any bad attributes C<import> croaks.
215b39c5158Smillert
216b39c5158Smillert(See L<"Package-specific Attribute Handling"> below.)
217b39c5158Smillert
218b39c5158Smillert=head2 Built-in Attributes
219b39c5158Smillert
220b39c5158SmillertThe following are the built-in attributes for subroutines:
221b39c5158Smillert
222b39c5158Smillert=over 4
223b39c5158Smillert
224b39c5158Smillert=item lvalue
225b39c5158Smillert
226b39c5158SmillertIndicates that the referenced subroutine is a valid lvalue and can
227b39c5158Smillertbe assigned to.  The subroutine must return a modifiable value such
228b39c5158Smillertas a scalar variable, as described in L<perlsub>.
229b39c5158Smillert
230898184e3SsthenThis module allows one to set this attribute on a subroutine that is
231898184e3Ssthenalready defined.  For Perl subroutines (XSUBs are fine), it may or may not
232898184e3Ssthendo what you want, depending on the code inside the subroutine, with details
233898184e3Ssthensubject to change in future Perl versions.  You may run into problems with
234898184e3Ssthenlvalue context not being propagated properly into the subroutine, or maybe
235898184e3Sstheneven assertion failures.  For this reason, a warning is emitted if warnings
236898184e3Ssthenare enabled.  In other words, you should only do this if you really know
237898184e3Ssthenwhat you are doing.  You have been warned.
238898184e3Ssthen
239b39c5158Smillert=item method
240b39c5158Smillert
241898184e3SsthenIndicates that the referenced subroutine
242898184e3Ssthenis a method.  A subroutine so marked
243b39c5158Smillertwill not trigger the "Ambiguous call resolved as CORE::%s" warning.
244b39c5158Smillert
2456fb12b70Safresh1=item prototype(..)
2466fb12b70Safresh1
2476fb12b70Safresh1The "prototype" attribute is an alternate means of specifying a prototype
2486fb12b70Safresh1on a sub.  The desired prototype is within the parens.
2496fb12b70Safresh1
2506fb12b70Safresh1The prototype from the attribute is assigned to the sub immediately after
2516fb12b70Safresh1the prototype from the sub, which means that if both are declared at the
2526fb12b70Safresh1same time, the traditionally defined prototype is ignored.  In other words,
2536fb12b70Safresh1C<sub foo($$) : prototype(@) {}> is indistinguishable from C<sub foo(@){}>.
2546fb12b70Safresh1
2556fb12b70Safresh1If illegalproto warnings are enabled, the prototype declared inside this
2566fb12b70Safresh1attribute will be sanity checked at compile time.
2576fb12b70Safresh1
258b8851fccSafresh1=item const
259b8851fccSafresh1
260b8851fccSafresh1This experimental attribute, introduced in Perl 5.22, only applies to
261b8851fccSafresh1anonymous subroutines.  It causes the subroutine to be called as soon as
262b8851fccSafresh1the C<sub> expression is evaluated.  The return value is captured and
263b8851fccSafresh1turned into a constant subroutine.
264b8851fccSafresh1
26591f110e0Safresh1=back
26691f110e0Safresh1
26791f110e0Safresh1The following are the built-in attributes for variables:
26891f110e0Safresh1
26991f110e0Safresh1=over 4
27091f110e0Safresh1
27191f110e0Safresh1=item shared
27291f110e0Safresh1
27391f110e0Safresh1Indicates that the referenced variable can be shared across different threads
27491f110e0Safresh1when used in conjunction with the L<threads> and L<threads::shared> modules.
27591f110e0Safresh1
276b39c5158Smillert=back
277b39c5158Smillert
278b39c5158Smillert=head2 Available Subroutines
279b39c5158Smillert
280b39c5158SmillertThe following subroutines are available for general use once this module
281b39c5158Smillerthas been loaded:
282b39c5158Smillert
283b39c5158Smillert=over 4
284b39c5158Smillert
285b39c5158Smillert=item get
286b39c5158Smillert
287b39c5158SmillertThis routine expects a single parameter--a reference to a
288b39c5158Smillertsubroutine or variable.  It returns a list of attributes, which may be
289b39c5158Smillertempty.  If passed invalid arguments, it uses die() (via L<Carp::croak|Carp>)
290b39c5158Smillertto raise a fatal exception.  If it can find an appropriate package name
291b39c5158Smillertfor a class method lookup, it will include the results from a
292b39c5158SmillertC<FETCH_I<type>_ATTRIBUTES> call in its return list, as described in
293b39c5158SmillertL<"Package-specific Attribute Handling"> below.
294b39c5158SmillertOtherwise, only L<built-in attributes|"Built-in Attributes"> will be returned.
295b39c5158Smillert
296b39c5158Smillert=item reftype
297b39c5158Smillert
298b39c5158SmillertThis routine expects a single parameter--a reference to a subroutine or
299b39c5158Smillertvariable.  It returns the built-in type of the referenced variable,
300b39c5158Smillertignoring any package into which it might have been blessed.
301b39c5158SmillertThis can be useful for determining the I<type> value which forms part of
302b39c5158Smillertthe method names described in L<"Package-specific Attribute Handling"> below.
303b39c5158Smillert
304b39c5158Smillert=back
305b39c5158Smillert
306b39c5158SmillertNote that these routines are I<not> exported by default.
307b39c5158Smillert
308b39c5158Smillert=head2 Package-specific Attribute Handling
309b39c5158Smillert
310b39c5158SmillertB<WARNING>: the mechanisms described here are still experimental.  Do not
311b39c5158Smillertrely on the current implementation.  In particular, there is no provision
312b39c5158Smillertfor applying package attributes to 'cloned' copies of subroutines used as
313b39c5158Smillertclosures.  (See L<perlref/"Making References"> for information on closures.)
314b39c5158SmillertPackage-specific attribute handling may change incompatibly in a future
315b39c5158Smillertrelease.
316b39c5158Smillert
317b39c5158SmillertWhen an attribute list is present in a declaration, a check is made to see
318b39c5158Smillertwhether an attribute 'modify' handler is present in the appropriate package
319b39c5158Smillert(or its @ISA inheritance tree).  Similarly, when C<attributes::get> is
320b39c5158Smillertcalled on a valid reference, a check is made for an appropriate attribute
321b39c5158Smillert'fetch' handler.  See L<"EXAMPLES"> to see how the "appropriate package"
322b39c5158Smillertdetermination works.
323b39c5158Smillert
324b39c5158SmillertThe handler names are based on the underlying type of the variable being
325b39c5158Smillertdeclared or of the reference passed.  Because these attributes are
326b39c5158Smillertassociated with subroutine or variable declarations, this deliberately
327b39c5158Smillertignores any possibility of being blessed into some package.  Thus, a
328b39c5158Smillertsubroutine declaration uses "CODE" as its I<type>, and even a blessed
329b39c5158Smillerthash reference uses "HASH" as its I<type>.
330b39c5158Smillert
331b39c5158SmillertThe class methods invoked for modifying and fetching are these:
332b39c5158Smillert
333b39c5158Smillert=over 4
334b39c5158Smillert
335b39c5158Smillert=item FETCH_I<type>_ATTRIBUTES
336b39c5158Smillert
337b39c5158SmillertThis method is called with two arguments:  the relevant package name,
338b39c5158Smillertand a reference to a variable or subroutine for which package-defined
339b39c5158Smillertattributes are desired.  The expected return value is a list of
340b39c5158Smillertassociated attributes.  This list may be empty.
341b39c5158Smillert
342b39c5158Smillert=item MODIFY_I<type>_ATTRIBUTES
343b39c5158Smillert
344b39c5158SmillertThis method is called with two fixed arguments, followed by the list of
345b39c5158Smillertattributes from the relevant declaration.  The two fixed arguments are
346b39c5158Smillertthe relevant package name and a reference to the declared subroutine or
347b39c5158Smillertvariable.  The expected return value is a list of attributes which were
348b39c5158Smillertnot recognized by this handler.  Note that this allows for a derived class
349b39c5158Smillertto delegate a call to its base class, and then only examine the attributes
350b39c5158Smillertwhich the base class didn't already handle for it.
351b39c5158Smillert
352b39c5158SmillertThe call to this method is currently made I<during> the processing of the
353b39c5158Smillertdeclaration.  In particular, this means that a subroutine reference will
354b39c5158Smillertprobably be for an undefined subroutine, even if this declaration is
355b39c5158Smillertactually part of the definition.
356b39c5158Smillert
357b39c5158Smillert=back
358b39c5158Smillert
359b39c5158SmillertCalling C<attributes::get()> from within the scope of a null package
360b39c5158Smillertdeclaration C<package ;> for an unblessed variable reference will
361b39c5158Smillertnot provide any starting package name for the 'fetch' method lookup.
362b39c5158SmillertThus, this circumstance will not result in a method call for package-defined
363b39c5158Smillertattributes.  A named subroutine knows to which symbol table entry it belongs
364b39c5158Smillert(or originally belonged), and it will use the corresponding package.
365b39c5158SmillertAn anonymous subroutine knows the package name into which it was compiled
366b39c5158Smillert(unless it was also compiled with a null package declaration), and so it
367b39c5158Smillertwill use that package name.
368b39c5158Smillert
369b39c5158Smillert=head2 Syntax of Attribute Lists
370b39c5158Smillert
371b39c5158SmillertAn attribute list is a sequence of attribute specifications, separated by
372b39c5158Smillertwhitespace or a colon (with optional whitespace).
373b39c5158SmillertEach attribute specification is a simple
374b39c5158Smillertname, optionally followed by a parenthesised parameter list.
375b39c5158SmillertIf such a parameter list is present, it is scanned past as for the rules
376b39c5158Smillertfor the C<q()> operator.  (See L<perlop/"Quote and Quote-like Operators">.)
377b39c5158SmillertThe parameter list is passed as it was found, however, and not as per C<q()>.
378b39c5158Smillert
379b39c5158SmillertSome examples of syntactically valid attribute lists:
380b39c5158Smillert
381b39c5158Smillert    switch(10,foo(7,3))  :  expensive
382b39c5158Smillert    Ugly('\(") :Bad
383b39c5158Smillert    _5x5
384b39c5158Smillert    lvalue method
385b39c5158Smillert
386b39c5158SmillertSome examples of syntactically invalid attribute lists (with annotation):
387b39c5158Smillert
388b39c5158Smillert    switch(10,foo()		# ()-string not balanced
389b39c5158Smillert    Ugly('(')			# ()-string not balanced
390b39c5158Smillert    5x5				# "5x5" not a valid identifier
391b39c5158Smillert    Y2::north			# "Y2::north" not a simple identifier
392b39c5158Smillert    foo + bar			# "+" neither a colon nor whitespace
393b39c5158Smillert
394b39c5158Smillert=head1 EXPORTS
395b39c5158Smillert
396b39c5158Smillert=head2 Default exports
397b39c5158Smillert
398b39c5158SmillertNone.
399b39c5158Smillert
400b39c5158Smillert=head2 Available exports
401b39c5158Smillert
402b39c5158SmillertThe routines C<get> and C<reftype> are exportable.
403b39c5158Smillert
404b39c5158Smillert=head2 Export tags defined
405b39c5158Smillert
406b39c5158SmillertThe C<:ALL> tag will get all of the above exports.
407b39c5158Smillert
408b39c5158Smillert=head1 EXAMPLES
409b39c5158Smillert
410b39c5158SmillertHere are some samples of syntactically valid declarations, with annotation
411b39c5158Smillertas to how they resolve internally into C<use attributes> invocations by
412b39c5158Smillertperl.  These examples are primarily useful to see how the "appropriate
413b39c5158Smillertpackage" is found for the possible method lookups for package-defined
414b39c5158Smillertattributes.
415b39c5158Smillert
416b39c5158Smillert=over 4
417b39c5158Smillert
418b39c5158Smillert=item 1.
419b39c5158Smillert
420b39c5158SmillertCode:
421b39c5158Smillert
422b39c5158Smillert    package Canine;
423b39c5158Smillert    package Dog;
424b39c5158Smillert    my Canine $spot : Watchful ;
425b39c5158Smillert
426b39c5158SmillertEffect:
427b39c5158Smillert
428b39c5158Smillert    use attributes ();
429b39c5158Smillert    attributes::->import(Canine => \$spot, "Watchful");
430b39c5158Smillert
431b39c5158Smillert=item 2.
432b39c5158Smillert
433b39c5158SmillertCode:
434b39c5158Smillert
435b39c5158Smillert    package Felis;
436b39c5158Smillert    my $cat : Nervous;
437b39c5158Smillert
438b39c5158SmillertEffect:
439b39c5158Smillert
440b39c5158Smillert    use attributes ();
441b39c5158Smillert    attributes::->import(Felis => \$cat, "Nervous");
442b39c5158Smillert
443b39c5158Smillert=item 3.
444b39c5158Smillert
445b39c5158SmillertCode:
446b39c5158Smillert
447b39c5158Smillert    package X;
448b39c5158Smillert    sub foo : lvalue ;
449b39c5158Smillert
450b39c5158SmillertEffect:
451b39c5158Smillert
452b39c5158Smillert    use attributes X => \&foo, "lvalue";
453b39c5158Smillert
454b39c5158Smillert=item 4.
455b39c5158Smillert
456b39c5158SmillertCode:
457b39c5158Smillert
458b39c5158Smillert    package X;
459b39c5158Smillert    sub Y::x : lvalue { 1 }
460b39c5158Smillert
461b39c5158SmillertEffect:
462b39c5158Smillert
463b39c5158Smillert    use attributes Y => \&Y::x, "lvalue";
464b39c5158Smillert
465b39c5158Smillert=item 5.
466b39c5158Smillert
467b39c5158SmillertCode:
468b39c5158Smillert
469b39c5158Smillert    package X;
470b39c5158Smillert    sub foo { 1 }
471b39c5158Smillert
472b39c5158Smillert    package Y;
473b39c5158Smillert    BEGIN { *bar = \&X::foo; }
474b39c5158Smillert
475b39c5158Smillert    package Z;
476b39c5158Smillert    sub Y::bar : lvalue ;
477b39c5158Smillert
478b39c5158SmillertEffect:
479b39c5158Smillert
480b39c5158Smillert    use attributes X => \&X::foo, "lvalue";
481b39c5158Smillert
482b39c5158Smillert=back
483b39c5158Smillert
484b39c5158SmillertThis last example is purely for purposes of completeness.  You should not
485b39c5158Smillertbe trying to mess with the attributes of something in a package that's
486b39c5158Smillertnot your own.
487b39c5158Smillert
488b39c5158Smillert=head1 MORE EXAMPLES
489b39c5158Smillert
490b39c5158Smillert=over 4
491b39c5158Smillert
492b39c5158Smillert=item 1.
493b39c5158Smillert
494b39c5158Smillert    sub MODIFY_CODE_ATTRIBUTES {
495b39c5158Smillert       my ($class,$code,@attrs) = @_;
496b39c5158Smillert
497b39c5158Smillert       my $allowed = 'MyAttribute';
498b39c5158Smillert       my @bad = grep { $_ ne $allowed } @attrs;
499b39c5158Smillert
500b39c5158Smillert       return @bad;
501b39c5158Smillert    }
502b39c5158Smillert
503b39c5158Smillert    sub foo : MyAttribute {
504b39c5158Smillert       print "foo\n";
505b39c5158Smillert    }
506b39c5158Smillert
507898184e3SsthenThis example runs.  At compile time
508898184e3SsthenC<MODIFY_CODE_ATTRIBUTES> is called.  In that
509b39c5158Smillertsubroutine, we check if any attribute is disallowed and we return a list of
510b39c5158Smillertthese "bad attributes".
511b39c5158Smillert
512b39c5158SmillertAs we return an empty list, everything is fine.
513b39c5158Smillert
514b39c5158Smillert=item 2.
515b39c5158Smillert
516b39c5158Smillert  sub MODIFY_CODE_ATTRIBUTES {
517b39c5158Smillert     my ($class,$code,@attrs) = @_;
518b39c5158Smillert
519b39c5158Smillert     my $allowed = 'MyAttribute';
520b39c5158Smillert     my @bad = grep{ $_ ne $allowed }@attrs;
521b39c5158Smillert
522b39c5158Smillert     return @bad;
523b39c5158Smillert  }
524b39c5158Smillert
525b39c5158Smillert  sub foo : MyAttribute Test {
526b39c5158Smillert     print "foo\n";
527b39c5158Smillert  }
528b39c5158Smillert
529b39c5158SmillertThis example is aborted at compile time as we use the attribute "Test" which
530898184e3Ssthenisn't allowed.  C<MODIFY_CODE_ATTRIBUTES>
531898184e3Ssthenreturns a list that contains a single
532b39c5158Smillertelement ('Test').
533b39c5158Smillert
534b39c5158Smillert=back
535b39c5158Smillert
536b39c5158Smillert=head1 SEE ALSO
537b39c5158Smillert
538b39c5158SmillertL<perlsub/"Private Variables via my()"> and
539b39c5158SmillertL<perlsub/"Subroutine Attributes"> for details on the basic declarations;
540b39c5158SmillertL<perlfunc/use> for details on the normal invocation mechanism.
541b39c5158Smillert
542b39c5158Smillert=cut
543