This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
doco improvement for attributes.pm
authorDavid Cantrell <david@cantrell.org.uk>
Mon, 27 Feb 2012 00:13:18 +0000 (16:13 -0800)
committerFather Chrysostomos <sprout@cpan.org>
Mon, 27 Feb 2012 00:13:41 +0000 (16:13 -0800)
It seems that many people have trouble understanding how to add custom
attributes to their subroutines.  Here's a doc patch that will
hopefully make things clearer:

ext/attributes/attributes.pm

index a883c04..fdc61e8 100644 (file)
@@ -313,6 +313,22 @@ 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.
 
+It is up to this method to store the list of attributes if they will be
+needed later, as well as checking for any errors.  In this example there
+are no error conditions, so we just store:
+
+  my %attrs;
+  sub MODIFY_CODE_ATTRIBUTES {
+    my($package, $subref, @attrs) = @_;
+    $attrs{ refaddr $subref } = \@attrs;
+    return;
+  }
+  sub FETCH_CODE_ATTRIBUTES {
+    my($package, $subref) = @_;
+    my $attrs = $attrs{ refaddr $subref };
+    return $attrs ? @$attrs : ();
+  }
+
 =back
 
 Calling C<attributes::get()> from within the scope of a null package