attributes man page on AIX

Man page or keyword search:  
man Server   4752 pages
apropos Keyword Search (all sections)
Output format
AIX logo
[printable version]

attributes(3)	       Perl Programmers Reference Guide		 attributes(3)

       attributes - get/set subroutine or variable attributes

	 sub foo : method ;
	 my ($x,@y,%z) : Bent = 1;
	 my $s = sub : method { ... };

	 use attributes ();    # optional, to get subroutine declarations
	 my @attrlist = attributes::get(\&foo);

	 use attributes 'get'; # import the attributes::get subroutine
	 my @attrlist = get \&foo;

       Subroutine declarations and definitions may optionally have attribute
       lists associated with them.  (Variable "my" 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 exam‐
       ple above is equivalent to the following:

	   use attributes __PACKAGE__, \&foo, 'method';

       The second example in the synopsis does something equivalent to this:

	   use attributes ();
	   my ($x,@y,%z);
	   attributes::->import(__PACKAGE__, \$x, 'Bent');
	   attributes::->import(__PACKAGE__, \@y, 'Bent');
	   attributes::->import(__PACKAGE__, \%z, 'Bent');
	   ($x,@y,%z) = 1;

       Yes, that's a lot of expansion.

       WARNING: 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.

       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 "Package-specific Attribute Handling" below.)

       The setting of subroutine attributes happens at compile time.  Variable
       attributes in "our" declarations are also applied at compile time.
       However, "my" variables get their attributes applied at run-time.  This
       means that you have to reach the run-time component of the "my" before
       those attributes will get applied.  For example:

	   my $x : Bent = 42 if 0;

       will neither assign 42 to $x nor will it apply the "Bent" attribute to
       the variable.

       An attempt to set an unrecognized attribute is a fatal error.  (The
       error is trappable, but it still stops the compilation within that
       "eval".)	 Setting an attribute with a name that's all lowercase letters
       that's not a built-in attribute (such as "foo") will result in a warn‐
       ing with -w or "use warnings 'reserved'".

       What "import" does

       In the description it is mentioned that

	 sub foo : method;

       is equivalent to

	 use attributes __PACKAGE__, \&foo, 'method';

       As you might know this calls the "import" function of "attributes" at
       compile time with these parameters: 'attributes', the caller's package
       name, the reference to the code and 'method'.

	 attributes->import( __PACKAGE__, \&foo, 'method' );

       So you want to know what "import" actually does?

       First of all "import" gets the type of the third parameter ('CODE' in
       this case).  "" checks if there is a subroutine called
       "MODIFY_<reftype>_ATTRIBUTES" in the caller's namespace (here: 'main').
       In this case a subroutine "MODIFY_CODE_ATTRIBUTES" is required. Then
       this method is called to check if you have used a "bad attribute".  The
       subroutine call in this example would look like

	 MODIFY_CODE_ATTRIBUTES( 'main', \&foo, 'method' );

       "MODIFY_<reftype>_ATTRIBUTES" has to return a list of all "bad
       attributes".  If there are any bad attributes "import" croaks.

       (See "Package-specific Attribute Handling" below.)

       Built-in Attributes

       The following are the built-in attributes for subroutines:

	   5.005 threads only!	The use of the "locked" attribute currently
	   only makes sense if you are using the deprecated "Perl 5.005
	   threads" implementation of threads.

	   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 method attribute below), Perl
	   ensures that any invocation of it implicitly locks its first argu‐
	   ment before execution.  When set on a non-method subroutine, Perl
	   ensures that a lock is taken on the subroutine itself before execu‐
	   tion.  The semantics of the lock are exactly those of one explic‐
	   itly taken with the "lock" operator immediately after the subrou‐
	   tine is entered.

	   Indicates that the referenced subroutine is a method.  This has a
	   meaning when taken together with the locked attribute, as described
	   there.  It also means that a subroutine so marked will not trigger
	   the "Ambiguous call resolved as CORE::%s" warning.

	   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.

       For global variables there is "unique" attribute: see "our" in perl‐

       Available Subroutines

       The following subroutines are available for general use once this mod‐
       ule has been loaded:

       get This routine expects a single parameter--a reference to a subrou‐
	   tine or variable.  It returns a list of attributes, which may be
	   empty.  If passed invalid arguments, it uses die() (via
	   Carp::croak) to raise a fatal exception.  If it can find an appro‐
	   priate package name for a class method lookup, it will include the
	   results from a "FETCH_type_ATTRIBUTES" call in its return list, as
	   described in "Package-specific Attribute Handling" below.  Other‐
	   wise, only built-in attributes will be returned.

	   This routine expects a single parameter--a reference to a subrou‐
	   tine 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 type value which
	   forms part of the method names described in "Package-specific
	   Attribute Handling" below.

       Note that these routines are not exported by default.

       Package-specific Attribute Handling

       WARNING: the mechanisms described here are still experimental.  Do not
       rely on the current implementation.  In particular, there is no provi‐
       sion for applying package attributes to 'cloned' copies of subroutines
       used as closures.  (See "Making References" in perlref for information
       on closures.)  Package-specific attribute handling may change incompat‐
       ibly in a future release.

       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 @ISA inheritance tree).	Similarly, when
       "attributes::get" is called on a valid reference, a check is made for
       an appropriate attribute 'fetch' handler.  See "EXAMPLES" to see how
       the "appropriate package" determination works.

       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 deliber‐
       ately ignores any possibility of being blessed into some package.
       Thus, a subroutine declaration uses "CODE" as its type, and even a
       blessed hash reference uses "HASH" as its type.

       The class methods invoked for modifying and fetching are these:

	   This method is called with two arguments:  the relevant package
	   name, and a reference to a variable or subroutine for which pack‐
	   age-defined attributes are desired.	The expected return value is a
	   list of associated attributes.  This list may be empty.

	   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 is 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.

	   The call to this method is currently made during the processing of
	   the declaration.  In particular, this means that a subroutine ref‐
	   erence will probably be for an undefined subroutine, even if this
	   declaration is actually part of the definition.

       Calling "attributes::get()" from within the scope of a null package
       declaration "package ;" 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 declara‐
       tion), and so it will use that package name.

       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 "q()" operator.  (See "Quote and Quote-
       like Operators" in perlop.)  The parameter list is passed as it was
       found, however, and not as per "q()".

       Some examples of syntactically valid attribute lists:

	   switch(10,foo(7,3))	:  expensive
	   Ugly('\(") :Bad
	   locked method

       Some examples of syntactically invalid attribute lists (with annota‐

	   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

       Default exports


       Available exports

       The routines "get" and "reftype" are exportable.

       Export tags defined

       The ":ALL" tag will get all of the above exports.

       Here are some samples of syntactically valid declarations, with annota‐
       tion as to how they resolve internally into "use attributes" invoca‐
       tions by perl.  These examples are primarily useful to see how the
       "appropriate package" is found for the possible method lookups for
       package-defined attributes.

       1.  Code:

	       package Canine;
	       package Dog;
	       my Canine $spot : Watchful ;


	       use attributes ();
	       attributes::->import(Canine => \$spot, "Watchful");

       2.  Code:

	       package Felis;
	       my $cat : Nervous;


	       use attributes ();
	       attributes::->import(Felis => \$cat, "Nervous");

       3.  Code:

	       package X;
	       sub foo : locked ;


	       use attributes X => \&foo, "locked";

       4.  Code:

	       package X;
	       sub Y::x : locked { 1 }


	       use attributes Y => \&Y::x, "locked";

       5.  Code:

	       package X;
	       sub foo { 1 }

	       package Y;
	       BEGIN { *bar = \&X::foo; }

	       package Z;
	       sub Y::bar : locked ;


	       use attributes X => \&X::foo, "locked";

       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.

		  my ($class,$code,@attrs) = @_;

		  my $allowed = 'MyAttribute';
		  my @bad = grep { $_ ne $allowed } @attrs;

		  return @bad;

	       sub foo : MyAttribute {
		  print "foo\n";

	   This example runs. At compile time "MODIFY_CODE_ATTRIBUTES" is
	   called. In that subroutine, we check if any attribute is disallowed
	   and we return a list of these "bad attributes".

	   As we return an empty list, everything is fine.

		my ($class,$code,@attrs) = @_;

		my $allowed = 'MyAttribute';
		my @bad = grep{ $_ ne $allowed }@attrs;

		return @bad;

	     sub foo : MyAttribute Test {
		print "foo\n";

	   This example is aborted at compile time as we use the attribute
	   "Test" which isn't allowed. "MODIFY_CODE_ATTRIBUTES" returns a list
	   that contains a single element ('Test').

       "Private Variables via my()" in perlsub and "Subroutine Attributes" in
       perlsub for details on the basic declarations; attrs for the obsoles‐
       cent form of subroutine attribute specification which this module
       replaces; "use" in perlfunc for details on the normal invocation mecha‐

perl v5.8.8			  2008-09-19			 attributes(3)
                             _         _         _ 
                            | |       | |       | |     
                            | |       | |       | |     
                         __ | | __ __ | | __ __ | | __  
                         \ \| |/ / \ \| |/ / \ \| |/ /  
                          \ \ / /   \ \ / /   \ \ / /   
                           \   /     \   /     \   /    
                            \_/       \_/       \_/ 
More information is available in HTML format for server AIX

List of man pages available for AIX

Copyright (c) for man pages and the logo by the respective OS vendor.

For those who want to learn more, the polarhome community provides shell access and support.

[legal] [privacy] [GNU] [policy] [cookies] [netiquette] [sponsors] [FAQ]
Polarhome, production since 1999.
Member of Polarhome portal.
Based on Fawad Halim's script.
Vote for polarhome
Free Shell Accounts :: the biggest list on the net