develooper Front page | perl.beginners | Postings from January 2002

who is the method cop?

From:
Skip Montanaro
Date:
January 7, 2002 10:29
Subject:
who is the method cop?
Message ID:
15417.59534.334459.66273@beluga.mojam.com

I'm trying to figure out Perl's object-oriented features.  I'm a long-time
Python programmer, so I'm well-versed in its notion of OO programming, but
Perl's method definition stuff seems a bit "loose".  All of my Perl
programming is in a Mason context, so that's where I'll pull an example.
Let's see, here's a simple enough example.  These two methods are from
HTML::Mason::ApacheHandler.

    sub new
    {
        my ($class,%options) = @_;
        my $interp = $options{interp} or die "HTML::Mason::Request::ApacheHandler::new: must specify interp\n";
        delete $options{interp};
        my $self = $class->SUPER::new(interp=>$interp);
        while (my ($key,$value) = each(%options)) {
            if (exists($reqfields{$key})) {
                $self->{$key} = $value;
            } else {
                die "HTML::Mason::Request::ApacheHandler::new: invalid option '$key'\n";
            }
        }
        return $self;
    }

    # Override flush_buffer to also call $r->rflush
    sub flush_buffer
    {
        my ($self, $content) = @_;
        $self->SUPER::flush_buffer($content);
        $self->apache_req->rflush;
    }

I understand the assignment to $self and $class.  What I don't understand is
how new and flush_buffer are associated with a specific class.  For example,
is there anything that keeps me from calling flush_buffer with an instance
of a class other than ApacheHandler or calling new with some other class?
Should the class author be doing some type checking?

Thanks,

-- 
Skip Montanaro (skip@pobox.com - http://www.mojam.com/)



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