here you are attempting to execute method process() from an object which is held in $self->{kiosk}. Now the latter is undefined as you never set it.
You can set the kiosk object inside $server after you create the kiosk like this:
my $server = MyWebServer->new($kiosk_port); print "INSTANTIATING KIOSK ==============================\n"; $kiosk = Kiosk->new($placard, $server, $debug_level); #### let the server know what the kiosk obj is: $server->{kiosk} = $kiosk;
That said, the "proper" way would be to create accessors or getter and setter methods to set the kiosk object and other parameters internal to a class with all the proper checks, if kiosk is empty and what not. The boilerplate code can be avoided and "properness" inserted automatically by using an OO system, for example Moose (edit: or Moo). It is not something that I ever used as I like my own OO style, but using such a system in a production environment gives you certain code-quality guarantees. Although you can entirely do without it (and skip a few kilos of cargo).
One of the aims of OO is to black-box your program units into classes which do complex things internally but provide you with the simplest possible API for telling them what to do and on what data. Like, initialise($kiosk);, is_connected(), do_transaction(10). Making a class with tens of internal variables all exposed defies using OO in the first place and soon complexity will either kill your program or introduce so many bugs. From your code: you don't know which variable is primary, like $self->{kiosk} and which is derived, $self->{kiosk_id} which I assume could be set via the kiosk object. Perl's OO beauty, in my opinion, is its light-weight-ness. But it allows a lot of things which are generally critisised like indiscriminately exposing variables.
So,
package Server; ... sub new { ... $self = {kiosk=>undef}; ... return $self; } sub kiosk { my $self = shift; my $m = shift; if( defined $m ){ die unless $self->_set_kiosk($m); } return $self->{kiosk} } # returns 0 on failure, 1 on success sub _set_kiosk { my $self = shift; my $akiosk = shift; if( ! $akiosk->connected() ){ return 0 } $self->{kiosk_id} = $akiosk->id(); print "setting the kiosk to this: " . $akiosk. "\n"; return 1; } sub do_transaction { my $self = shift; return 0 unless defined $self->{kiosk}; ... }
OO is one of the great tools programmers were given ever (that and the hashtable!). Use it wisely and to your advantage.
bw, bliako
Edit: this is related: Inheritable configuration options.... but with default values?
In reply to Re: Mass Class Confusion - Who calls what how?
by bliako
in thread Mass Class Confusion - Who calls what how?
by holandes777
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |