Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi Light-Angel,

Before I even try to figure out what your error is, I want to suggest you consider refactoring your code, at least in future programs.

Edit: I see you're using POE modules, which I don't use, nor have installed. I'll take a few more minutes to see if I spot anything, but I'm afraid I won't be able to run this.

For example, doing this:

if (...) { if (...) { if (...) { if (...) { .... } else { return 0; } } else { return 0; } } else { return 0; } } else { return 0; }
Is simply unreadable. And my example only goes to 4 levels; your code goes to 7!

If you reverse the logic, and do the return 0 blocks first, it's a heck of a lot easier to read and maintain:

($msg =~ /^([^\s]+)/) or return 0; $alias = $1; ($msg =~ /-address="([^"]+)"/) or return 0; $address = $1; ($msg =~ /-port="([^"]+)"/) or return 0; $port = $1; ($msg =~ /-nick="([^"]+)"/) or return 0; $nick = $1; ($msg =~ /-password="([^"]+)"/) or return 0; $password = $1; ($msg =~ /-prefix="([^"]+)"/) or return 0; $prefix = $1; ($msg =~ /-auto_connect="([^"]+)"/) or return 0; $auto_connect = $1;

You could even take it a step further, and make it data-driven. It's not as short as the first rewrite, but it has the advantage of grouping functionality together, so you can see what's being matched all in one section, and what's being assigned to in another section:

# Regular expressions to apply my $a_regexes = [ qr/^([^\s]+)/, qr/-address="([^"]+)"/, qr/-port="([^"]+)"/, qr/-nick="([^"]+)"/, qr/-password="([^"]+)"/, qr/-prefix="([^"]+)"/, qr/-auto_connect="([^"]+)"/, ]; # Construct an array of resulting regex captures my @results = ( ); # Save each pattern (returning if missing from regex) foreach my $regex (@$a_regexes) { if ($msg !~ /$regex/) { return 0; } push @results, $1; } # Assign to the desired variables my ($alias, $address, $port, $nick, $password, $prefix, $auto_connect) = @results;

Update 2:

Here's an even terser suggestion for capturing the tokens, since (other than the first regex) they all have the same essential format. Define the list of tokens to capture, and build each regex from that:

# Define the tokens we wish to extract my @tokens = qw( address port nick password prefix auto_connect ); # First token is a special case my $alias = ($msg =~ /^(\S+)\s*/)? $1: ""; $alias or return 0; # Construct an array of resulting regex captures my @results = ( ); # Construct the regex from each token, and capture the value foreach my $token (@tokens) { ($msg =~ /-${token}="([^"]+)"/ or return 0; push @results, $1; } # Assign to the desired variables my ($address, $port, $nick, $password, $prefix, $auto_connect) = @r +esults;
say  substr+lc crypt(qw $i3 SI$),4,5

In reply to Re: Passing message between 2 sockets by golux
in thread Passing message between 2 sockets by Light-Angel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-24 05:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found