rastoboy has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Brothers! I'm a novice at object oriented perl (and OO programming in general), but it is starting to make proper sense to me.

But what I'm wanting to want to do is put comments in my objects. Some of the objects I'm building are rather large and complex. I have comments in my code, but I "feel" like it would be "right" to add some comments within the object itself to explain things to anyone who cared to dump it and inspect it. (Or for my future self, of course. I'm inclined to like the guy and don't want to give him a hard time).

I can't find any discussion of such a thing, or any obvious modules in cpan. That always makes me suspicious of my motives.

Am I wrong to want this? Is there something out there already to help me to it really nicely?

Or is this my first cpan contribution waiting to happen? :-)

Edit: I've been thinking in terms of just adding a hashref, perhaps named '#', and have its value be the comment. Or just sticking the comment in there as a hashref key.

Replies are listed 'Best First'.
Re: Putting comments in an object
by moritz (Cardinal) on Apr 02, 2010 at 10:51 UTC
    Why not put the comments into the documentation of the class? It feels like unnecessary baggage to carry it around in each object.
    Perl 6 - links to (nearly) everything that is Perl 6.
      Thanks to you all. You've given me a lot to think about.

      After pondering, it does seem to me that there's no reason not to just put it in the documentation, with a sample dump and all if I must. POD or whatever, within the object doesn't seem as clever now as it did at first! lol.

      Thanks!

Re: Putting comments in an object
by LanX (Saint) on Apr 02, 2010 at 12:53 UTC
    do you mean docstrings ?

    I would strongly recommend a solution where docstrings and pod are easily mirrored, but I'm searching myself.

    As a suggestion, some people do things (roughly) like this:

    $class::doc=<<"__doc__"; =pod docstring bla bvla bla =cut __doc__
    (didn't test if this code really works, ATM I'm in a hurry)

    Cheers Rolf

      Yes, this works. I've used the technique to write documented lists:

      my @items = map { /^=item (.*)/ ? $1 : () } split /\n/, <<'=cut'; =begin =over 4 =item Hello The first item must always be C<Hello> =item World The second item must always be C<World> =back =cut print "Items: @items\n";
Re: Putting comments in an object
by doug (Pilgrim) on Apr 02, 2010 at 12:41 UTC

    Ungh. That sounds ugly. Embrace POD as the one true way to document your code. If you want a module to return its documention (which, at face value, sounds strange) then convert your module name to a path name and run pod2* on that file. Basically class Foo::Bar's get translated to system 'pod2html', 'Foo/Bar.pm' or something of that sort.

    I believe you could capture pod2text's output and return that as a string if you really want the class to return something.

    - doug

Re: Putting comments in an object
by Jenda (Abbot) on Apr 02, 2010 at 15:55 UTC

    If I understand right, you want those comments to be available when you dump or stringify the object, right? In that case you do not need to keep the documentation in all those instances, but you should rather overload the "freezer" so that whenever you attemtp to dump the object it adds the comments to whereever it thinks is best. See $Data::Dumper::Freezer in Data::Dumper.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Re: Putting comments in an object
by BrowserUk (Patriarch) on Apr 02, 2010 at 10:56 UTC

    What sort of comments are you thinking of adding?