Writing to a socket between many modules

G

Gareth Curtis

Hi all,

I wonder if someone can give me some advice.

I am using WxWindows with Perl and have a script with many different
packages. I'd like each of these packages to be able to write to a socket.

In the main.pl file I have:

my $socket_handle = connect_server();

Then each package is able to see the global variable, I assume this is ok.

The connect_server is a module containing the code to connect to the
server using the Socket.pm module. I struggled with this for ages and
finally worked out that the socket is actually a kind of file and to
return() a file I had to do this:

my $socket_handle = *SOCKET_HANDLE; # Set the $socket_handle variable to
the SOCKET_HANDLE (pass file type handle with *)

return($socket_handle);

This seems to work. My question is; is this the right way to do this? Or
will it cause problems later maybe? If not, what is the correct way to do
this.

Many thanks for any help,

Gareth.
 
U

Uri Guttman

GC> The connect_server is a module containing the code to connect to
GC> the server using the Socket.pm module. I struggled with this for
GC> ages and finally worked out that the socket is actually a kind of
GC> file and to return() a file I had to do this:

GC> my $socket_handle = *SOCKET_HANDLE; # Set the $socket_handle
GC> variable to the SOCKET_HANDLE (pass file type handle with *)

GC> return($socket_handle);

use the IO::Socket module. it is much easier to use than the low level
Socket.pm module. it will return a socket handle directly to you and not
need a type glob and the other stuff you are obviously doing there.

GC> This seems to work. My question is; is this the right way to do
GC> this? Or will it cause problems later maybe? If not, what is the
GC> correct way to do this.

i am not sure about what your question means. getting a socket is one
thing (and i told you a better way to do it). using it is another
thing. sharing a single socket among many modules is fine as long as
they all have access to the same handle. but how do you go about sharing
that handle? a package global will work but that is not a good
idea. making all the modules use an OO interface to this handle is a
possibility. but you would then need to create an instance of this
object in each module that needs it or again have a global object. i bet
you have what we call an XY problem. your design is set so you think you
have to do the i/o from each module. maybe a redesign is in order. i
have never seen a design where socket i/o has to be done all over the
place. that is prone to bugs and has maintenance issues. maybe your
modules should be called and just return their desired output to a
parent module which can do all the socket i/o. then you have a higher
level view of things and much better control over your data and i/o.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top