Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Passing message between 2 sockets

by golux (Chaplain)
on Apr 11, 2016 at 01:30 UTC ( #1160076=note: print w/replies, xml ) Need Help??


in reply to Passing message between 2 sockets

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1160076]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2020-11-30 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?