Part of the API I'm writing involves a lot of class member variables. For class variables, I use file-scoped lexicals (from man perltooc):
package Some_Class; my %ClassData = ( CData1 => "", CData2 => "", ); for my $datum (keys %ClassData) { no strict "refs"; *$datum = sub { shift; # XXX: ignore calling class/object $ClassData{$datum} = shift if @_; return $ClassData{$datum}; }; }
So what's the big deal, you ask? Well, it lets me do things like this:
my $foo = Foo::new();
$foo->CData1("foo");
etc.
So, generating documentation for these is pretty trivial. I do inline POD, so it'll look something like this:
=head1 MEMBERS
=over
=item b<CData1([$CData1])>
=item b<CData2([$CData2])>
=cut
Or you can replace the $CDatax with a variable type, if you want.
I'm working on a class right now with about 20 member variables. I wanted to generate documentation for all of the member variables, so that you could do perldoc MyClass to get the list instead of having to look at code.
There is a very easy way to do this! I use vim as my editor, so I did the following:
- Visually select all of the contents of the %ClassData hash.
- Copy the text (y) and paste it (p) into a documentation block
- Highlight all of the pasted text
- Unindent it (<<)
- :s/\(.*\) =>.*/=item B<\1([$\1])>\r
- Enter
0 comments:
Post a Comment