Writing to a socket between many modules

Discussion in 'Perl Misc' started by Gareth Curtis, Aug 5, 2003.

  1. 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.
     
    Gareth Curtis, Aug 5, 2003
    #1
    1. Advertising

  2. Gareth Curtis

    Uri Guttman Guest

    >>>>> "GC" == Gareth Curtis <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Aug 5, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,856
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    971
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    552
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    666
    Steve Holden
    Feb 1, 2009
  5. Clemens Wyss
    Replies:
    3
    Views:
    637
    Sandor Szücs
    May 18, 2009
Loading...

Share This Page