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

I've just started playing around with OOP. I learn best by observing, and have not been able to find any well written code that has multiple packages/objects interracting with each other. Anyone have an idea where I should further direct my search or where I should look for this?

Thanks;

Replies are listed 'Best First'.
Re: Object Interaction
by tadman (Prior) on May 09, 2001 at 19:06 UTC
    What kind of "interaction" are you intending to perform? There are a number of modules on CPAN that are OO, such as CGI.pm, DBI, or LWP and many others, and these can "interact" by definition, though not without some effort on the part of the programmer. Interaction between modules is usually by design, and as such, you can't really expect two modules written by two different people for two different purposes to interact spontaneously. They may be compatible, but it is in the program that you write where the interaction occurs.

    A good example of interaction is the LWP system, the Web "browser" library for Perl. To implement this, there are a variety of objects which are used to represent everything from a server connection (HTTP::Request) through to a URL (URI), and many things inbetween. The library is very comprehensive, and highly abstracted, so if LWP is too confusing, you can always use LWP::Simple which makes it a snap to 'get' Web pages.

    Perhaps some sample code would better illustrate this. This example grabs all the links from the Perl Monks page, something that HTML::LinkExtor does quite well, so this is for educational purposes only:
    #!/usr/bin/perl -w use strict; use HTML::Parser; use HTTP::Request; use HTTP::Response; use LWP::UserAgent; use URI; # Create a LWP::UserAgent object which can be used to fetch pages my $agent = new LWP::UserAgent(); # Create a URI, which represents a location on the WWW my $uri = new URI ("http://www.perlmonks.com/"); # Create a HTML::Parser, used to process the HTML my $parser = new HTML::Parser ( api_version => 3, start_h => [ \&Start, 'tagname,attr' ], ); # Define a behavior, used by HTML::Parser when it encounters # the "start" of a tag (i.e. a declaration like '<X>', where # the tagname would be 'x', as it is always passed as lower-case) sub Start { my ($tagname,$attr) = @_; if ($tagname eq 'a') { print "$tagname href=$attr->{href}\n"; } } # Make a download request for the page, which is requesting # a 'GET' of the URL specified earlier my $request = new HTTP::Request ( GET => $uri->as_string() ); # Tell the LWP::UserAgent to process this request, and # capture the response. my $response = $agent->request ($request); # Feed the response to the HTML::Parser, which because of # the customized 'Start' function, will do something useful. $parser->parse ($response->content());
Re: Object Interaction
by frankus (Priest) on May 09, 2001 at 21:01 UTC

    Do you mean things like inheritance and extending, interfaces and the like? I know of only one place where this is dealt with to a complete level: Damian Conway's book. A right rivetting read.

    I don't wish to detract from Perl's OO capabilities but it's not something I'd learn from another's work.

    Look around the monestary for the gotcha's. How AUTOLOAD tangles with inheritance and the like. Perl is a lovely language and too obliging to be implementing strict OO without a lot of additional functions added by the user. Polymorphism and the like that is another bonus of OO is rather lost on Perl as all Perl functions are polymorphic. But that is not to say that OO Perl isn't lovely.

    --

    Brother Frankus.

    ¤

      The book he speaks of can be found here

      And I agree, it is an incredible book

Re: Object Interaction
by t'mo (Pilgrim) on May 10, 2001 at 17:01 UTC

    If you're able to 'speak' some other programming languages besides Perl, you might want to try Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, and take ideas from that. (You won't be able to take code examples, but the ideas about how groups of objects work together are the key here.)