Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: P2P Golf: MoleSter

by blazar (Canon)
on Dec 16, 2004 at 15:10 UTC ( #415364=note: print w/ replies, xml ) Need Help??


in reply to P2P Golf: MoleSter

Well, the actual code is:

$p=shift;$a=shift;i(shift);socket S,2,1,6;bind S,&a($a);listen S,5;$/=undef;while(@ARGV&&($_="$p $a f".shift)||accept(C,S)&&($_=<C>)& +&close C){m!^(.*?) (.*?) ([e-i])([^/]*)/!s&&$1 eq$p&&&$3($2,$4,$');}sub e{ope +n F,'>',$_[1];print F $_[2];close F}sub f{&s($_,@_)for keys %k}sub g{open(F,'<',$_[1])&&&s($_[0],$a,"e$_[1]",<F>);close F}sub h{&s($_[0],$_,'i')for keys %k}sub i{$k{$_[0]}=1}sub a{$_[0]=~/:/;pack'CxnC4x8',2,$',split'\.',$`}sub s{socket X,2,1,6;$w=shift;if(connect X,&a($w)){print X "$p $_[0] $_[1]/$_[2]";close X}else{undef $k{$p}}}
Of course I'm not the one "Who wants to take the first crack at golfing this down" but giving a peek into the first few lines I notice that there's plenty of room for golfing. In fact I'm only a mediocre golfer, but golfers for example generally pop() rather than shift().

Or, for example, C<undef$/;> takes one keystroke less than C<$/=undef;>. But then (e.g.) C<$/=$_> will do and is even shorter.

Again, selecting a randomly picked line, I'm a big fan of the three-arguments form of open(), but C<open(F,'<',$_[1])> can be replaced by C<open F,pop>.

As a side note, since we're talking golf here, well one doesn't generally care much about good programming practices in this context. But the author of the original code's systematic (&-form of) calling of subs clearly tells what his programming skills can be...


Comment on Re: P2P Golf: MoleSter
Select or Download Code
Re^2: P2P Golf: MoleSter
by dragonchild (Archbishop) on Dec 16, 2004 at 15:16 UTC
    • You actually cannot use pop instead of shift in the first line unless you rearrange the argument list order. The same goes for most of the subroutines.
    • You cannot use open F,pop; because there are 3 args coming in, not 2.
    • The &x() syntax is actually the golfed version in several places, particularly &$3($2,$4,$');. Now, in others, it can be removed. But, I sincerely doubt it has anything to do with the author's skills as a programmer. Personally, I'm impressed with a P2P client in under 1000 characters.

    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.

      You actually cannot use pop instead of shift in the first line unless you rearrange the argument list order.

      If there's no ordering dependencies between things (in this case &a and &i don't seem to depend on one being called before the others, and neither references $p) you can if you just reverse the order you pop rather than the argument order . . .

      i(pop);$a=a(pop);$p=pop;...

      Update: D'oh, missed the @ARGV later on. Never mind me.

        Except that @ARGV is used later on in the while() loop control statement.

        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.

      You actually cannot use pop instead of shift in the first line unless you rearrange the argument list order. The same goes for most of the subroutines.
      WOW! Thank you so much for having pointed out so: I would have never imagined myself...

      What I wanted to stress is that even for a mediocre golfer like me at a first glance the code, despite some evident conciseness still was largely suboptimal from this point of view for just applying a standard golfing technique like using pop() instead of shift() (and reversing the order of the arguments - pointing out the obvious, just to keep you satisfied) wherever applicable would have saved quite a lot charachters.

      But then it's well known that experienced golfers use any sort of more refined tricks. For example I can remember one case in which there were four arguments to take from the cmd line and it was practically mandatory to assign these to variables so they used something along the lines of this (since the golf rules did not require strictures or warnings): $$_=pop for a..d;

      You cannot use open F,pop; because there are 3 args coming in, not 2.
      Huh?!?
      The &x() syntax is actually the golfed version in several places, particularly &$3($2,$4,$');. Now, in others, it can be removed. But, I sincerely doubt it has anything to do with the author's skills as a programmer. Personally, I'm impressed with a P2P client in under 1000 characters.
      Well, indeed if we're talking symrefs then you're right. It may also save some chars in other situations. Generally with modern enough perls the &-syntax does not do what one expects, and as a matter of a fact unless there's a really good reason to adopt it (and indeed in golfing and obfuscation games there may be some well beyond "regular" ones), code that abuses it reveals that he/she who wrote it is still programming Perl4. I'm not sure if this is the case with this particular program, but this is the impression I got at a quick glance.
        >> You cannot use open F,pop; because there are 3 args coming in, not 2.

        Huh?!?

        The function in question is

        sub g{ open(F,'<',$_[1])&&&s($_[0],$a,"e$_[1]",<F>); close F }

        On first glance, it looks like you can replace $_[1] with pop for a 2-character savings. However, look at where g() is called - &$3($2,$4,$'). There are three scalars in @_, not two. The fact that g() only uses two is irrelevant.

        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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-12-29 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (185 votes), past polls