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

your solution is correct. Passing in the $kiosk object makes it accessible to the message handler routine

print "INSTANTIATING KIOSK ==============================\n"; my $kiosk = Kiosk->new($placard, $server, $debug_level); # start the server on port __ print "INSTANTIATING SERVER port $kiosk_port ========================= +=====\n"; my $server = MyWebServer->new($kiosk_port); $server->run(); $server->{kiosk} = $kiosk;

this part in HTTP::Server::Simple::CGI; sees it just fine and responds properly ... so THANK YOU.

sub handle_request { my $self = shift; my $cgi = shift; my $json_string = $cgi->param('POSTDATA'); # { "type":"cardCheck", "date":"1482307795601", "lang": "EN", +"machineName":"PLUSMAKM", "cardNumber":"1234567890", "PIN": "1234" } print "json_string=$json_string\n"; $json_dict = decode_json $json_string; print "json_dict=" . Dumper($json_dict) . "\n"; my $response = $self->{kiosk}->process($json_dict); print "response=$response\n"; return $response; }

there was one issue in fully implementing your solution (the code worked without it). When you add the "new" method to HTTP::Server::Simple::CGI;object as follows you get an execution error

sub new { my $self = {}; $self->{class} = $class; $self->{kiosk} = undef; bless($self,$class); return $self; }

Can't locate object method "run" via package "main" at http_server.pl line 56, <CONF> line 35. line 56 is

$server->run();

this was the original reason I requested help. Now that I see the code a bit more I guess the question is "how do you exend the HTTP::Server::Simple::CGI; object, but that is another topic. SO I thank you for an excelent easy solution and, if you have any ideas about the ast question, I would love to hear it so I can gain more knowledge about OO in Perl.

Replies are listed 'Best First'.
Re^3: Mass Class Confusion - Who calls what how?
by bliako (Abbot) on Feb 22, 2020 at 12:01 UTC

    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

      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