Plan 9 from Bell Labs’s /usr/web/sources/contrib/gabidiaz/root/sys/man/2perl/attributes

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  | will give a
.\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
.\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
.    de IX
.    tm Index:\\$1\t\\n%\t"\\$2"
..
.    nr % 0
.    rr F
.\}
.\"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "attributes 3"
.TH attributes 3 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide"
.SH "NAME"
attributes \- get/set subroutine or variable attributes
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 3
\&  sub foo : method ;
\&  my ($x,@y,%z) : Bent = 1;
\&  my $s = sub : method { ... };
.Ve
.PP
.Vb 2
\&  use attributes ();    # optional, to get subroutine declarations
\&  my @attrlist = attributes::get(\e&foo);
.Ve
.PP
.Vb 2
\&  use attributes 'get'; # import the attributes::get subroutine
\&  my @attrlist = get \e&foo;
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
Subroutine declarations and definitions may optionally have attribute lists
associated with them.  (Variable \f(CW\*(C`my\*(C'\fR declarations also may, but see the
warning below.)  Perl handles these declarations by passing some information
about the call site and the thing being declared along with the attribute
list to this module.  In particular, the first example above is equivalent to
the following:
.PP
.Vb 1
\&    use attributes __PACKAGE__, \e&foo, 'method';
.Ve
.PP
The second example in the synopsis does something equivalent to this:
.PP
.Vb 6
\&    use attributes ();
\&    my ($x,@y,%z);
\&    attributes::->import(__PACKAGE__, \e$x, 'Bent');
\&    attributes::->import(__PACKAGE__, \e@y, 'Bent');
\&    attributes::->import(__PACKAGE__, \e%z, 'Bent');
\&    ($x,@y,%z) = 1;
.Ve
.PP
Yes, that's a lot of expansion.
.PP
\&\fB\s-1WARNING\s0\fR: attribute declarations for variables are still evolving.
The semantics and interfaces of such declarations could change in
future versions.  They are present for purposes of experimentation
with what the semantics ought to be.  Do not rely on the current
implementation of this feature.
.PP
There are only a few attributes currently handled by Perl itself (or
directly by this module, depending on how you look at it.)  However,
package-specific attributes are allowed by an extension mechanism.
(See \*(L"Package\-specific Attribute Handling\*(R" below.)
.PP
The setting of subroutine attributes happens at compile time.
Variable attributes in \f(CW\*(C`our\*(C'\fR declarations are also applied at compile time.
However, \f(CW\*(C`my\*(C'\fR variables get their attributes applied at run\-time.
This means that you have to \fIreach\fR the run-time component of the \f(CW\*(C`my\*(C'\fR
before those attributes will get applied.  For example:
.PP
.Vb 1
\&    my $x : Bent = 42 if 0;
.Ve
.PP
will neither assign 42 to \f(CW$x\fR \fInor\fR will it apply the \f(CW\*(C`Bent\*(C'\fR attribute
to the variable.
.PP
An attempt to set an unrecognized attribute is a fatal error.  (The
error is trappable, but it still stops the compilation within that
\&\f(CW\*(C`eval\*(C'\fR.)  Setting an attribute with a name that's all lowercase
letters that's not a built-in attribute (such as \*(L"foo\*(R") will result in
a warning with \fB\-w\fR or \f(CW\*(C`use warnings 'reserved'\*(C'\fR.
.Sh "Built-in Attributes"
.IX Subsection "Built-in Attributes"
The following are the built-in attributes for subroutines:
.IP "locked" 4
.IX Item "locked"
Setting this attribute is only meaningful when the subroutine or
method is to be called by multiple threads.  When set on a method
subroutine (i.e., one marked with the \fBmethod\fR attribute below),
Perl ensures that any invocation of it implicitly locks its first
argument before execution.  When set on a non-method subroutine,
Perl ensures that a lock is taken on the subroutine itself before
execution.  The semantics of the lock are exactly those of one
explicitly taken with the \f(CW\*(C`lock\*(C'\fR operator immediately after the
subroutine is entered.
.IP "method" 4
.IX Item "method"
Indicates that the referenced subroutine is a method.
This has a meaning when taken together with the \fBlocked\fR attribute,
as described there.  It also means that a subroutine so marked
will not trigger the \*(L"Ambiguous call resolved as CORE::%s\*(R" warning.
.IP "lvalue" 4
.IX Item "lvalue"
Indicates that the referenced subroutine is a valid lvalue and can
be assigned to. The subroutine must return a modifiable value such
as a scalar variable, as described in perlsub.
.PP
For global variables there is \f(CW\*(C`unique\*(C'\fR attribute: see \*(L"our\*(R" in perlfunc.
.Sh "Available Subroutines"
.IX Subsection "Available Subroutines"
The following subroutines are available for general use once this module
has been loaded:
.IP "get" 4
.IX Item "get"
This routine expects a single parameter\*(--a reference to a
subroutine or variable.  It returns a list of attributes, which may be
empty.  If passed invalid arguments, it uses \fIdie()\fR (via Carp::croak)
to raise a fatal exception.  If it can find an appropriate package name
for a class method lookup, it will include the results from a
\&\f(CW\*(C`FETCH_\f(CItype\f(CW_ATTRIBUTES\*(C'\fR call in its return list, as described in
\&\*(L"Package\-specific Attribute Handling\*(R" below.
Otherwise, only built-in attributes will be returned.
.IP "reftype" 4
.IX Item "reftype"
This routine expects a single parameter\*(--a reference to a subroutine or
variable.  It returns the built-in type of the referenced variable,
ignoring any package into which it might have been blessed.
This can be useful for determining the \fItype\fR value which forms part of
the method names described in \*(L"Package\-specific Attribute Handling\*(R" below.
.PP
Note that these routines are \fInot\fR exported by default.
.Sh "Package-specific Attribute Handling"
.IX Subsection "Package-specific Attribute Handling"
\&\fB\s-1WARNING\s0\fR: the mechanisms described here are still experimental.  Do not
rely on the current implementation.  In particular, there is no provision
for applying package attributes to 'cloned' copies of subroutines used as
closures.  (See \*(L"Making References\*(R" in perlref for information on closures.)
Package-specific attribute handling may change incompatibly in a future
release.
.PP
When an attribute list is present in a declaration, a check is made to see
whether an attribute 'modify' handler is present in the appropriate package
(or its \f(CW@ISA\fR inheritance tree).  Similarly, when \f(CW\*(C`attributes::get\*(C'\fR is
called on a valid reference, a check is made for an appropriate attribute
\&'fetch' handler.  See \*(L"\s-1EXAMPLES\s0\*(R" to see how the \*(L"appropriate package\*(R"
determination works.
.PP
The handler names are based on the underlying type of the variable being
declared or of the reference passed.  Because these attributes are
associated with subroutine or variable declarations, this deliberately
ignores any possibility of being blessed into some package.  Thus, a
subroutine declaration uses \*(L"\s-1CODE\s0\*(R" as its \fItype\fR, and even a blessed
hash reference uses \*(L"\s-1HASH\s0\*(R" as its \fItype\fR.
.PP
The class methods invoked for modifying and fetching are these:
.IP "\s-1FETCH_\s0\fItype\fR_ATTRIBUTES" 4
.IX Item "FETCH_type_ATTRIBUTES"
This method receives a single argument, which is a reference to the
variable or subroutine for which package-defined attributes are desired.
The expected return value is a list of associated attributes.
This list may be empty.
.IP "\s-1MODIFY_\s0\fItype\fR_ATTRIBUTES" 4
.IX Item "MODIFY_type_ATTRIBUTES"
This method is called with two fixed arguments, followed by the list of
attributes from the relevant declaration.  The two fixed arguments are
the relevant package name and a reference to the declared subroutine or
variable.  The expected return value as a list of attributes which were
not recognized by this handler.  Note that this allows for a derived class
to delegate a call to its base class, and then only examine the attributes
which the base class didn't already handle for it.
.Sp
The call to this method is currently made \fIduring\fR the processing of the
declaration.  In particular, this means that a subroutine reference will
probably be for an undefined subroutine, even if this declaration is
actually part of the definition.
.PP
Calling \f(CW\*(C`attributes::get()\*(C'\fR from within the scope of a null package
declaration \f(CW\*(C`package ;\*(C'\fR for an unblessed variable reference will
not provide any starting package name for the 'fetch' method lookup.
Thus, this circumstance will not result in a method call for package-defined
attributes.  A named subroutine knows to which symbol table entry it belongs
(or originally belonged), and it will use the corresponding package.
An anonymous subroutine knows the package name into which it was compiled
(unless it was also compiled with a null package declaration), and so it
will use that package name.
.Sh "Syntax of Attribute Lists"
.IX Subsection "Syntax of Attribute Lists"
An attribute list is a sequence of attribute specifications, separated by
whitespace or a colon (with optional whitespace).
Each attribute specification is a simple
name, optionally followed by a parenthesised parameter list.
If such a parameter list is present, it is scanned past as for the rules
for the \f(CW\*(C`q()\*(C'\fR operator.  (See \*(L"Quote and Quote-like Operators\*(R" in perlop.)
The parameter list is passed as it was found, however, and not as per \f(CW\*(C`q()\*(C'\fR.
.PP
Some examples of syntactically valid attribute lists:
.PP
.Vb 4
\&    switch(10,foo(7,3))  :  expensive
\&    Ugly('\e(") :Bad
\&    _5x5
\&    locked method
.Ve
.PP
Some examples of syntactically invalid attribute lists (with annotation):
.PP
.Vb 5
\&    switch(10,foo()             # ()-string not balanced
\&    Ugly('(')                   # ()-string not balanced
\&    5x5                         # "5x5" not a valid identifier
\&    Y2::north                   # "Y2::north" not a simple identifier
\&    foo + bar                   # "+" neither a colon nor whitespace
.Ve
.SH "EXPORTS"
.IX Header "EXPORTS"
.Sh "Default exports"
.IX Subsection "Default exports"
None.
.Sh "Available exports"
.IX Subsection "Available exports"
The routines \f(CW\*(C`get\*(C'\fR and \f(CW\*(C`reftype\*(C'\fR are exportable.
.Sh "Export tags defined"
.IX Subsection "Export tags defined"
The \f(CW\*(C`:ALL\*(C'\fR tag will get all of the above exports.
.SH "EXAMPLES"
.IX Header "EXAMPLES"
Here are some samples of syntactically valid declarations, with annotation
as to how they resolve internally into \f(CW\*(C`use attributes\*(C'\fR invocations by
perl.  These examples are primarily useful to see how the \*(L"appropriate
package\*(R" is found for the possible method lookups for package-defined
attributes.
.IP "1." 4
Code:
.Sp
.Vb 3
\&    package Canine;
\&    package Dog;
\&    my Canine $spot : Watchful ;
.Ve
.Sp
Effect:
.Sp
.Vb 2
\&    use attributes ();
\&    attributes::->import(Canine => \e$spot, "Watchful");
.Ve
.IP "2." 4
Code:
.Sp
.Vb 2
\&    package Felis;
\&    my $cat : Nervous;
.Ve
.Sp
Effect:
.Sp
.Vb 2
\&    use attributes ();
\&    attributes::->import(Felis => \e$cat, "Nervous");
.Ve
.IP "3." 4
Code:
.Sp
.Vb 2
\&    package X;
\&    sub foo : locked ;
.Ve
.Sp
Effect:
.Sp
.Vb 1
\&    use attributes X => \e&foo, "locked";
.Ve
.IP "4." 4
Code:
.Sp
.Vb 2
\&    package X;
\&    sub Y::x : locked { 1 }
.Ve
.Sp
Effect:
.Sp
.Vb 1
\&    use attributes Y => \e&Y::x, "locked";
.Ve
.IP "5." 4
Code:
.Sp
.Vb 2
\&    package X;
\&    sub foo { 1 }
.Ve
.Sp
.Vb 2
\&    package Y;
\&    BEGIN { *bar = \e&X::foo; }
.Ve
.Sp
.Vb 2
\&    package Z;
\&    sub Y::bar : locked ;
.Ve
.Sp
Effect:
.Sp
.Vb 1
\&    use attributes X => \e&X::foo, "locked";
.Ve
.PP
This last example is purely for purposes of completeness.  You should not
be trying to mess with the attributes of something in a package that's
not your own.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
\&\*(L"Private Variables via \fImy()\fR\*(R" in perlsub and
\&\*(L"Subroutine Attributes\*(R" in perlsub for details on the basic declarations;
attrs for the obsolescent form of subroutine attribute specification
which this module replaces;
\&\*(L"use\*(R" in perlfunc for details on the normal invocation mechanism.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.