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

Hi all, I am trying to learn what I can about OO with perl, and have read all the tutorials I can find (I know how to make my barnyard animals talk) and the camel book, but what I really need is a real-life sample (something from CPAN) I can study that shows very simple OO implementations. Most of the modules that look interesting to me wind up looking more complex than I want when I look inside.

Does anyone have a suggestion of a fun, simple, useful (I mostly do web stuff so anything web related would be most useful to me) module using OO that I could disect for study?

Replies are listed 'Best First'.
Re: Real life OO examples
by nothingmuch (Priest) on Oct 18, 2002 at 10:09 UTC
    I'll write one at the top of my head. It's really silly tho...
    package HTMLTag; sub new { bless { tag => '', mainval => '', subval => '' },$_[0]; } sub tag { my $self = shift; $self->{tag} = $_[0] if $_[0]; # set if applicable $self->{tag}; # return } sub val { my $self = shift; $self->{mainval} = $_[0] if $_[0]; $self->{mainval}; } sub extra { my $self = shift; $self->{subval} = $_[0] if $_[0]; $self->{subval}; } sub str { my $self = shift; my $ret = join($self->{tag},"<",">"); if ($self->{tag} eq 'a'){ $ret .= join(""," href='",$self->{mainval},">",$self->{subval} +,"</a>"); } elsif ($self->{tag} eq 'img'){ $ret .= join(""," src='",$self->{mainval},"' alt='",$self->{su +bval},"'>"); # not sure about actual HTML correctness... i forget } } 1; # keep Ryszard happy ;-)
    And an example implementing:
    use HTMLTag; my $link = new HTMLTag; $link->tag('a'); $link->val('img.jpg'); $link->extra('A picture of my dog'); my $image = new HTMLTag; $image->tag('img'); $image->val('img2.jpg'); $image->extra('My cat'); print $link->str(),$image->str();

    Ofcourse, the beauty of objects is complexity hidden behind a simple look. That's why it's hard to find simple ones...

    -nuffin
    zz zZ Z Z #!perl
Re: Real life OO examples
by gjb (Vicar) on Oct 18, 2002 at 11:10 UTC

    This is an implementation of a dictionary class. One could simply do this with a hash, but that would result in ugly code (not that I'm claiming that the code below isn't uglu, BTW).

    An additional and important benefit is that one can have as many dictionaries (i.e. instances of the class Dicitonary) as one needs.

    Hope this helps, -gjb-

    Example follows:
Re: Real life OO examples
by MZSanford (Curate) on Oct 18, 2002 at 12:59 UTC

    I wrote the original versiion of Image::OrgChart quite some time ago, and have never really gone though the work of making it more complex (or more flexible). It does quite a bit with GD, but i think it is pretty followable (is that a word ? is now ).

    While Net::FTP does a bit of complex stuff, if you know the FTP protocol, it is pretty clear.


    from the frivolous to the serious
Re: Real life OO examples
by demerphq (Chancellor) on Oct 18, 2002 at 11:12 UTC
    Most of the modules that look interesting to me wind up looking more complex than I want when I look inside.

    OO is a way of simplifying versatilety. So while an OO module may be more complex than its non OO equivelenet, it becomes much easier to extend and the like. A little bit of complexity at the beginning makes the overall project less complex by the end.

    For a good example of OO have a look at the File::Spec family of modules. They arent web but they are a good example of practical use of OO.

    --- demerphq
    my friends call me, usually because I'm late....

      Actually, File::Spec is one of the worst examples in the Perl distro, because it's all about class methods, and no instances, and alters global behavior with global switches instead of carrying differing states around in instance variables.

      For distro-only examples, see the IO::File hierarchy. A little messy, but the inheritance, class, instance, and constructor methods are all pretty interesting and a good use of inheritance (and even dare I say "multiple inheritance").

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Oooh. (No pun intended.) Thanks for the clarification. I have to admit that I was thinking mostly on the exposed interface level and didn't consider the innerds at all. Which on reflection upon the OP was reasonably dumb. :-)

        But as a partial justification I was thinking of the way that you have one module with a clearly defined interface, that then automatically uses the appropriate overrides to provide a per OS functionality. While the class based interface is a touch annoying (ive taken to doing

        use constant FS=>'File::Spec'; my ($v,$p,$f)=FS->splitpath($0);
        as a shortcut) the versatility is not.

        Thanks for the pointer though, while not feeling that I need that much extra OO exposure (I do almost pure OO perl, and I've read your and TOMC's and TheDamians books and writings on the subjects already) I will certainly take the time to review these modules to see if theres anything more for me to learn (probably ;-).

        Incidentally, I wonder if pointing out Tie::SecureHash to the OP is worthwhile or not?

        --- demerphq
        Nice disclaimer :-)

Re: Real life OO examples
by rbc (Curate) on Oct 18, 2002 at 16:38 UTC
    At the risk of looking like a show off, you might want to
    look at my Lunar Lander video game.
    It is fun, simple and I think a good example of OO Perl.
    It is not web related though.