Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

MoleSter update - Golf

by cLive ;-) (Prior)
on Jan 07, 2005 at 01:55 UTC ( [id://420148]=perlmeditation: print w/replies, xml ) Need Help??

Last month, hossman posted about MoleSter. At the time is was 625 chars. It's now down to 466:
$/=$_;$,=shift;$w=$a=shift;$k{+shift}=1;socket S,2,1,6;bind S,&a;for(l +isten S,5;$SIG{ALRM}=\&i;m! (\S+) ([e-i])([^/]*)/!s&&($k{$w=$1}=$,eq$`)&&&$2 +){alarm 9;(accept(C,S),alarm 0)?read C,$_,1e6:($_="$, $a f".shift)}sub i{}sub +t{socket C,2,1,6;$k{$w}&&=(connect C,&a)?print C"$, ".pop:0;close C}sub h{t"$_ +i/"for keys%k}sub a{$w=~/:/;pack'CxnC4x8',2,$',split'\.',$`}sub f{$w=$_,t"$1 +$3/"for keys%k}sub e{open C,'>',$3;print C $'}sub g{open(C,'<',$3)&&t"$a e$3/" +.<C>;&h}
Just having a quick glance, I *think* I've knocked another 9 characters off:
sub a{$w=~/:/;pack'CxnC4x8',2,$',split'\.',$`}sub i{}sub t{socket C,2, +1,6;$k{$w}&&=(connect C,a)?print C"$, ".pop:0;close C}sub h{t"$_ i/"f +or keys%k}sub f{$w=$_,t"$1 $3/"for keys%k}sub e{open C,'>',$3;print C + $'}sub g{open(C,'<',$3)&&t"$a e$3/".<C>;h}$/=$_;$,=shift;$w=$a=shift +;$k{+shift}=1;socket S,2,1,6;bind S,a;for(listen S,5;$SIG{ALRM}=\&i;m +! (\S+) ([e-i])([^/]*)/!s&&($k{$w=$1}=$,eq$`)&&&$2){alarm 9;(accept(C +,S),alarm 0)?read C,$_,1e6:($_="$, $a f".shift)}

but I'm not sure because I haven't tried to get the original to do anything useful :)

So, do my changes still leave it working? And can anyone rip out a few more characters?

cLive ;-)

update: OK, damn, this is harder than it looks. Hmmm.

Replies are listed 'Best First'.
Re: MoleSter update - Golf
by dragonchild (Archbishop) on Jan 07, 2005 at 13:53 UTC
    You cannot change how @ARGV is processed because one of the features of the app is to be able to launch it and have it look for files right off the bat. That's what that shift is there for within the for-loop. By changing to use pop, you have to now put the filenames you want to find, then the connection args, as opposed to the connection args before the optional filenames.

    As for the three characters you chop by removing ampersands from two calls to a() and one call to h(), that also doesn't work. The reason for the ampersands is to pass on @_. Removing the ampersands doesn't do that, as demonstrated by this snippet:

    sub a { print "'@_'\n"; } sub b { &a } sub c { a } b( 'foo' ); c( 'foo' ); -------- 'foo' ''

    Additionally, you actually gained characters by not linebreaking at whitespace. If you look at the original, all five linebreaks were at places where whitespace was required. Matt Skala (the author) and I* spent hours figuring out how to arrange the functions so that every linebreak was consuming some piece of whitespace while still allowing for the use of the ampersand notation. It's a lot harder than it looks.

    I'm sure there's 2-3 characters that can be shaved, but I'll be damned if I know where they are ...

    * I'm the Rob Kinyon he mentions on his page.

    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.

      Ah, good point. Serves me right for not reading the docs :)

      cLive ;-)

Re: MoleSter update - Golf
by zentara (Archbishop) on Jan 07, 2005 at 14:13 UTC
    Just for info, this was just mentioned again on slashdot, generating another slew of comments on what is a line, and what is actually shorter. TinyP2P

    I'm not really a human, but I play one on earth. flash japh
      Yes, but TinyP2P uses a library, making it considerably *larger*. The only thing MoleSter relies on is TCP/IP, I believe.

      cLive ;-)

        I vote for MoleSter too, as a better example. Of course, MoleSter dosn't ask for a password, that's a drawback. I bet I could get Net::EasyTCP to make a simple connection, with password, in just a few lines.

        I'm not really a human, but I play one on earth. flash japh
Re: MoleSter update - Golf
by pijll (Beadle) on Jan 07, 2005 at 22:08 UTC
    I haven't tried the code either, but I may have found some improvements (based on the original code):
    $/=$_;($,,$a,$c,@_)=@_;$k{$c}=$w=$a;socket S,2,1,6;bind S,&a;for(liste +n S,5;$SIG{ALRM}=i;m! (\S+) ([e-i])(.*?)/!s&&($k{$w=$1}=$`eq$,)&&&$2){al +arm 9;(accept(C,S),alarm 0)?read C,$_,1e6:($_="$, $a f".shift)}sub i{}sub +t{socket C,2,1,6;$k{$w}&&=connect(C,&a)&&print C"$, ".pop;close C}sub h{t"$_ i/ +"for keys k}sub a{$w=~/:/;pack'CxnC4x8',2,$',split'\.',$`}sub f{$w=$_,t"$1 +$3/"for keys k}sub e{open C,'>',$3;print C $'}sub g{open(C,'<',$3)&&t"$a e$3/" +.<C>;&h}
    which is 10 bytes shorter. Again, I haven't tested it, but most of it should work... I hope.

    Why is the match in line 2 done with m!..!s? In the original code, there is no '.', so the s modifier is useless. It may be necessary in my code, however.

    Eugene

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://420148]
Approved by wolfger
Front-paged by wolfger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found