Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

MoleSter update - Golf

by cLive ;-) (Parson)
on Jan 07, 2005 at 01:55 UTC ( #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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://420148]
Approved by wolfger
Front-paged by wolfger
[Corion]: ambrus: I think both of AnyEvent and Prima are pretty tight in their memory management because they both are cooperative multitasking and (I think) both use the Perl memory management for managing things
[Corion]: ambrus: And for Windows, I don't think that Prima knows if there still are messages queued for an object (in the Windows message loop). Finding that out would take lots of effort for little gain
[ambrus]: And even if this works, I'm still not sure you can't get double timeouts from a Timer.
[ambrus]: Corion: well Prima::Object says something like that the cleanup method will send an onDestory message and that you can't get more messages after cleanup, or something.
[Corion]: ambrus: Yeah - I don't think the deep source dive will be necessary if things are implemented as simple as they could be :)) And hopefully I won't need (more) timely object destruction. I can update the screen at 60Hz and hopefully even do HTTP ...
[Corion]: ... transfers in the background. Now that I think about it, this maybe even means that I can run the OpenGL filters on Youtube input :)
[ambrus]: Corion: I mentioned that the unix event loop of Prima always wakes up at least once every 0.2 seconds. Have you found out whether the win32 event loop of Prima does that too?
[Corion]: ambrus: Hmm - I would assume that the onDestroy message is sent from the destructor and doesn't go through the messageloop, but maybe it is sent when a window gets destroyed but all components are still alive...
[ambrus]: Corion: partly deep source dive, partly just conservative coding even if it adds an overhead.
[Corion]: ambrus: Hmm - no, I haven't looked at wakeup intervals ... I wonder why it should want to wakeup periodically because it gets a lot of messages from the Windows message loop (on Windows obviously)

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2016-12-09 10:27 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (150 votes). Check out past polls.