in reply to My Code Is Functional...But Not Tidy :(

This is the perfect moment for a Factory pattern. Write a function that returns IO::Socket::Inet objects, but you only pass the parameters along.

Your factory, is an engine of sorts. How it figures out what object you want is up to you. You can make configuration files, as someone suggested and your factoury would do, Factory->new( 'http://test.com' ) for port 80, or Factory->new('smtp://test.com').

Or you can write a Factory that takes all the parameters.

All in all, it'd reduce the code down to ...

$web = Factory->new( some parameters, a url, or numbers and hosts );
$smtp = ....
$something = ....

But otherwise, it's not THAT ugly in truth. YOu formated your code, it's easy to read. The factory thing is all i can really suggest.
  • Comment on Re: My Code Is Functional...But Not Tidy :(

Replies are listed 'Best First'.
Re: Re: My Code Is Functional...But Not Tidy :(
by Anonymous Monk on Apr 24, 2003 at 17:16 UTC
    Isn't that what IO::Socket::Inet::new is already doing? I don't see how your factory method would be doing anything different. And for what it's worth, the original poster's code is some of the cleanest Perl I've ever seen.
      Well, if you integrate configuration files into the factory, your code becomes cleaner. Now lets say you stop using IO::Socket::Inet but switch to something else, you don't have to change from using IO::Socket everywhere, just change how the factory works.
      It is clean, yes. But, it's not very extensible, configurable, or customizable. It doesn't take advantage of shared snippets to make maintenance easier. That's what most of the responses have been about. Taking it to the next level, if that's what's desirable and desired.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.