in reply to Re^2: Mass Class Confusion - Who calls what how?
in thread Mass Class Confusion - Who calls what how?

You will be benefited from a Perl OO tutorial, here is one picked at random http://wwwacs.gantep.edu.tr/docs/perl-ebook/ch19.htm. There is also the official tutorial: perlootut

I assume you have read Re: Mass Class Confusion - Who calls what how?

To inherit from a class just use (after the package declaration):

use parent 'HTTP::Server::Simple::CGI';

There is also something similar in HTTP::Server::Simple's documentation...

But do you want that? Or do you want to instantiate a HTTP::Server::Simple::CGI object in your server and then call its run() method via your own run() whenever needed like so:

sub new { my ($class, $params) = @_; $self = {server_simple_cgi => undef, ...}; bless $self => $class; $params = {} unless defined $params; $self->{$_} = $params->{$_} for keys %$params; if( ! defined $self->{server_simple_cgi} ){ $self->{server_simple_cgi} = HTTP::Server::Simple->new(); } ... } sub run { my ($self, $params) = @_; # do something with params # and then run the server, it must be a valid object by now $self->{server_simple_cgi}->run() }

In this way you keep total control of your server and allow callers to control it only via certain API calls you provide (like the above run()) which may do additional checks, filter out things, before calling the server's, "real" run(). Note that this IS NOT about security (e.g. who is allowed to call what). Because a caller could still call $self->{server_simple_cgi}->run() (there are ways to avoid that: Objects with Private Variables). I am talking about the clarity and simplicity of the API you are building.

bw, bliako

Replies are listed 'Best First'.
Re^4: Mass Class Confusion - Who calls what how?
by bliako (Abbot) on Feb 23, 2020 at 11:05 UTC

    This is a much better Perl OO primer than the one I previously linked: https://www.perl.com/article/25/2013/5/20/Old-School-Object-Oriented-Perl/ . (I am glad to see that it sends parameters to functions as a hashtable by reference instead of by value).

    But I forgot to mention one important detail. Huge really. If the inherited (child) class has its own constructor (overriding - or more brutally, overwriting - the parent's constructor) then you must call in it the parent's constructor yourself before you do any more initialisation. Like so:

    package Child; use parent 'Parent'; sub new { my ($class, $params) = @_; # call parent's constructor first with the given parameters # the parent should ideally filter out the parameters # that do not concern it and ignore them rather than complaining a +bout illegal params my $self = $class->SUPER::new($params); # at this point $self has all Parent's initialisation # (whatever was done and set in its constructor). # Now do local initialisation on $self which is Parent's self (a { +} in most cases) ... $self->{child_extra_property_1} = 12; # for example ... # and bless to Child - actually that's a re-bless because calling # Parent's constructor blessed $self to be an object of class 'Par +ent' # so now make it of class 'Child' bless $self => $class; return $self; }

    bw, bliako