Wiggins has asked for the wisdom of the Perl Monks concerning the following question:
Please be tolerant, and read through to the end. I am trying to wrap
my head arround some OO Perl that doesn't match the style of any of
the examples I can find. The code ths that Msg.pm from
"Advanced Perl Programming By Sriram Srinivasan 1st Edition 1997".
Only some parts are in the book, but it is all tied together in
" ftp://ftp.oreilly.com/published/orielly/nutshell/advanved_perl/examples.tar.gz".
The calling code in "msgdemo.pl":
'new_server()' is called with only 3 arguments, not 4! But it works. The '$pkg' is used a bit later in this code:
"Advanced Perl Programming By Sriram Srinivasan 1st Edition 1997".
Only some parts are in the book, but it is all tied together in
" ftp://ftp.oreilly.com/published/orielly/nutshell/advanved_perl/examples.tar.gz".
Puzzlement #1)
This code comes from Msg.pm. It is the first sub in the file,
following some glocal hashes and such.
Connect lists 4 paramaters, 3 are used here.# Send side routines sub connect { my ($pkg, $to_host, $to_port, $rcvd_notification_proc) = @_; # Create a new internet socket my $sock = IO::Socket::INET->new ( PeerAddr => $to_host, PeerPort => $to_port, Proto => 'tcp', Reuse => 1);
The calling code in "msgdemo.pl":
use Msg; ... foreach $prog (@ARGV) { my $conn = Msg->connect($host, $port, \&rcvd_msg_from_server); die "Client could not connect to $host:$port\n" unless $conn; print "Connection successful.\n";
'new_server()' is called with only 3 arguments, not 4! But it works. The '$pkg' is used a bit later in this code:
From the comment, an object was created. The reference is the anonymous hash with 2 members, and the class name is what? $pkg appears to be an undefined scalar?# Create a connection end-point object my $conn = bless { sock => $sock, rcvd_notification_proc => $rcvd_notification_proc, }, $pkg;
Puzzlement #2
Further down in the code is a small subroutine that sets the flush
value to true for the 'conn':
From the prior 'bless', a $conn has no methods, but thesub send_now { my ($conn, $msg) = @_; _enqueue ($conn, $msg); $conn->_send (1); # 1 ==> flush ?????? }
looks like _send is a method of the $conn object. Is that a shorthand or alternate form of$conn->_send (1);
of a Perl convention to have a non object method work with this object._send ($conn,1);
This --server code listens on only one port, and I am trying to generalize it to manage multiple listen sockets and support UDP as well as TCP.
It is always better to have seen your target for yourself, rather than depend upon someone else's description.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Perl semi-object without a constructor
by tobyink (Canon) on Feb 20, 2013 at 21:29 UTC | |
by Wiggins (Hermit) on Feb 21, 2013 at 13:51 UTC | |
by tobyink (Canon) on Feb 21, 2013 at 17:37 UTC | |
Re: Perl semi-object without a constructor
by arnaud99 (Beadle) on Feb 20, 2013 at 19:19 UTC | |
Re: Perl semi-object without a constructor
by Anonymous Monk on Feb 20, 2013 at 19:35 UTC | |
by Wiggins (Hermit) on Feb 20, 2013 at 20:14 UTC | |
by dsheroh (Monsignor) on Feb 21, 2013 at 09:43 UTC | |
Re: Perl semi-object without a constructor
by sundialsvc4 (Abbot) on Feb 21, 2013 at 14:50 UTC | |
Re: Perl semi-object without a constructor
by TomDLux (Vicar) on Feb 21, 2013 at 15:26 UTC |
Back to
Seekers of Perl Wisdom