develooper Front page | perl.perl5.porters | Postings from February 2012

doco improvement for attributes.pm

Thread Next
From:
David Cantrell
Date:
February 16, 2012 09:07
Subject:
doco improvement for attributes.pm
Message ID:
20120216170651.GA3469@bytemark.barnyard.co.uk
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:

$ git diff cb82babdfb85b0a0ace048c92704350c138b3bb7 e7d545658a0257e62a390c5269cf8902d8f27412
diff --git a/ext/attributes/attributes.pm b/ext/attributes/attributes.pm
index a883c04..fdc61e8 100644
--- a/ext/attributes/attributes.pm
+++ b/ext/attributes/attributes.pm
@@ -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

-- 
David Cantrell | Godless Liberal Elitist

  Irregular English:
    ladies glow; gentlemen perspire; brutes, oafs and athletes sweat

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About