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

Re^2: P2P Golf: MoleSter

by dragonchild (Archbishop)
on Dec 17, 2004 at 14:24 UTC ( #415633=note: print w/ replies, xml ) Need Help??


in reply to Re: P2P Golf: MoleSter
in thread P2P Golf: MoleSter

I got a reply from the author and worked a "little" on it this morning. I'm down to 470 characters over 6 lines. There's a few bugs and several security holes fixed. Fixing them cost a few bytes, but I think it's worth it, overall. I mean, what use is showing Python we can beat them in program size if the program sucks? We have to show them we can write secure network software in 6 lines or less. *grins*

The author is working on revamping the while() loop and moving to use read() instead of <> for the socket. But, I don't have that version, so I'll work with what I have.

sub f{for$w(keys%k){&t}}$/=$_;($,,$w=$a,%k)=splice@ARGV,0,3;socket S,2 +,1,6;bind S,&a;sub i{$k{$w}=1}sub a{$w=~/:/;pack'CxnC4x8',2,$',split'\.',$`}sub +t{socket X,2,1,6;if(connect X,a){print X "$, $_[0] $_[1]/$_[2]";close}else{dele +te$k{$w}}} listen S,5;while(@ARGV&&($_="$, $a f".shift)||accept(C,S)&&($_=<C>)&&c +lose){ m!(\S*) ([e-i])([^/]*)/!s&&$,eq$`&&&$2($w=$1,$3,$')}sub e{open F,'>',$ +3;print F $';close}sub g{open(F,'<',$3)&&t$a,"e$3", <F>;close F}sub h{t$_,i for +keys%k}

This code is sooo completely untested. :-) The readable version ...

sub f{ for$w(keys%k){&t} } $/=$_; ($,,$w=$a,%k)=splice@ARGV,0,3; socket S,2,1,6; bind S,&a; sub i{ $k{$w}=1 } sub a{ $w=~/:/; pack'CxnC4x8',2,$',split'\.',$` } sub t{ socket X,2,1,6; if(connect X,a){ print X "$, $_[0] $_[1]/$_[2]"; close }else{ delete$k{$w} } } listen S,5; while(@ARGV&&($_="$, $a f".shift)||accept(C,S)&&($_=<C>)&&close){ m!(\S*) ([e-i])([^/]*)/!s&&$,eq$`&&&$2($w=$1,$3,$') } sub e{ open F,'>',$3; print F $'; close } sub g{ open(F,'<',$3)&&t$a,"e$3", <F>; close F } sub h{ t$_,i for keys%k }

Update: After jonadab's suggestions and some reorganization, we save 11 characters (down to 459 over 6 lines):

sub f{for$w(keys%k){&t}}$/=$_;($,,$w=$a,%k)=splice@ARGV,0,3;socket S,2 +,1,6;bind S,&a;sub i{$k{$w}=1}sub a{$w=~/:/;pack'CxnC4x8',2,$',split'\.',$`}sub +t{socket X,2,1,6;connect(X,a)?print(X "$, $_[0] $_[1]/$_[2]")&&close:delete$k{$ +w}}listen S,5;while(@ARGV&&($_="$, $a f".shift)||accept(C,S)&&($_=<C>)&&close){s +ub h{t$_,i for keys%k}m!(\S*) ([e-i])([^/]*)/!s&&$,eq$`&&&$2($w=$1,$3,$')}sub e{o +pen F,'>',$3;print F $';close}sub g{open(F,'<',$3)&&t$a,"e$3",<F>;close F} }

Yes, there's a subroutine definition in the while-loop. "perl -MO=Deparse" tells me it's ok.

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.


Comment on Re^2: P2P Golf: MoleSter
Select or Download Code
Re: P2P Golf: MoleSter
by jonadab (Parson) on Dec 17, 2004 at 15:16 UTC

    Wow, there's clearly an evil genius at work here. The splice is an interesting conversion. The thing you're doing calling i in sub h is slightly confusing, but I understand what you're doing with the regex in the while loop now, which I somehow missed last night. (Oh, I see, it was front-anchored in last night's version. That must be one of the bug fixes. But, won't $` contain the separating slash now? I'm still confused there. You've golfed it beyond my ability to follow it.)

    • You've still got the verbose if/else blocks in sub t. Converting to the trinary operator saves, if my calculations are correct, three strokes if you have to parenthesize the middle part, five if you don't. Am I missing something?
    • You've gone back to the three-argument open; is that one of the bug fixes you mentioned? I thought it was semantically the same... and if not, I want to understand the difference for reasons that have nothing to do with obfuscation, golf, or P2P.
    • I assume the delete is a bugfix, to purge dead peers from %k entirely. Makes sense.
    • The space after the comma in sub g, what's the reasoning behind that?
      In some semblance of order ...
      • The thing you're doing calling i in sub h is slightly confusing, ...

        I have no idea why any of the functions do what they do. I just make them do it with less characters.

      • The regex changes aren't a bugfix, and there is no separating slash in the first two values - they are separated by a space (which is why \S* is better than .*?). So, no slash in $`.
      • I hadn't gotten to the trinary yet - it probably does save a few characters. I'll do it and put out an update.
      • If you have open(F, ">$_[1]") and the filename is ">foo" ... what happens? The 3-arg open() prevents that.
      • The space after the comma is my eyes glazing over. I'll fix it in the update with the trinary.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        If you have open(F, ">$_[1]") and the filename is ">foo" ... what happens? The 3-arg open() prevents that.

        Gah. I keep forgetting that *nix allows shell metacharacters in filenames. (I cut my teeth on PC-DOS 3.3, which, whatever its other problems, does not permit that particular form of lossage.)


        "In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2014-09-23 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (220 votes), past polls