I wanted to make something distinctly perl, and despite its many powers, perl is best known for its unmatched text manipulation capabilities. Therefore, I endevored to write a binary clock program based upon string matching. It operates as a finite state machine, where the current state is the binary string representing each digit. No arithmatic takes place.

I was able to construct the core of the clock in one horrendously (beautiful?) long line of code,with a couple of extra lines for initilization and printing.

The current time can be set using command line aurguments like:

perl 438277.pl 0000 0011 0001 0111

The example above sets the current time to 3:17. The clock defaults to 1:00 if no command line aurguments are provided.

use warnings;use strict; (@ARGV eq 4)?(($",$~,$:,$.) = @ARGV,$^=$_='0000'): ($"=$_=$^=$.=$:='0000',$~='0001');{$|=1;select $,,$,,$,,1; ((s:0000:0001:)?():((s:0001:0010:)?():((s:0010:0011:)?():((s:0011:0100 +:)?():((s:0100:0101:)?():((s:0101:0110:)?():((s:0110:0111:)?():((s:01 +11:1000:)?():((s:1000:1001:))))))))))?():((s:1001:0000:)?((($^=~s:000 +0:0001:)?():(($^=~s:0001:0010:)?():(($^=~s:0010:0011:)?():(($^=~s:001 +1:0100:)?():(($^=~s:0100:0101:))))))?():(($^=~s:0101:0000:)?((($.=~s: +0000:0001:)?():(($.=~s:0001:0010:)?():(($.=~s:0010:0011:)?():(($.=~s: +0011:0100:)?():(($.=~s:0100:0101:)?():(($.=~s:0101:0110:)?():(($.=~s: +0110:0111:)?():(($.=~s:0111:1000:)?():(($.=~s:1000:1001:))))))))))?() +:(($.=~s:1001:0000:)?((($:=~s:0000:0001:)?():(($:=~s:0001:0010:)?():( +($:=~s:0010:0011:)?():(($:=~s:0011:0100:)?():(($:=~s:0100:0101:)))))) +?():(($:=~s:0101:0000:)?((($~=~s:0000:0001:)?():(($~=~s:0001:0010:)?( +):(($~=~s:0010:0011:)?(($"=='0001')?($~=$",$"='0000'):()):(($~=~s:001 +1:0100:)?():(($~=~s:0100:0101:)?():(($~=~s:0101:0110:)?():(($~=~s:011 +0:0111:)?():(($~=~s:0111:1000:)?():(($~=~s:1000:1001:))))))))))?():(( +$~=~s:1001:0000:)?(($"=~s:0000:0001:)?():($"=~s:0001:0000:)):())):()) +):())):())):()), (print "\r",$",' ',$~,':',$:,' ',$.,':',$^,' ',$_)&&redo}
If you really want time to fly, change the '1' in the 'select $,,$,,$,,1;' statement to '0'.

UPDATE: Something went wrong when I posted the code the first time. I have replaced it with code that should work as advertized.

Replies are listed 'Best First'.
Re: Binary Clock Implemented Through String Matching
by belg4mit (Prior) on Mar 12, 2005 at 15:09 UTC
    It's more of a binary representation of a normal clock, seeing as you use a nybble per digit. Here's a cleaned up version (did you mean to post to obfuscated code instead?), I replaced the space in the output with a . because the eye was drawn to it too much.
    use strict;use warnings;$|=1; (@ARGV eq 4) ? ( ($",$~,$:,$.) = @ARGV,$^=$_='0000') : ($"=$_=$^=$.=$: +='0000',$~='0001'); { select undef,undef,undef,1;0 || s:0000:0001: || s:0001:0010: || s:0010:0011: || s:0011:0100: || s:0100:0101: || s:0101:0110: || s:0110:0111: || s:0111:1000: || s:1000:1001: || s:1001:0000: &&$^=~s:0000:0001: ||$^=~s:0001:0010: ||$^=~s:0010:0011: ||$^=~s:0011:0100: ||$^=~s:0100:0101: ||$^=~s:0101:0000: &&$.=~s:0000:0001: ||$.=~s:0001:0010: ||$.=~s:0010:0011: ||$.=~s:0011:0100: ||$.=~s:0100:0101: ||$.=~s:0101:0110: ||$.=~s:0110:0111: ||$.=~s:0111:1000: ||$.=~s:1000:1001: ||$.=~s:1001:0000: &&$:=~s:0000:0001: ||$:=~s:0001:0010: ||$:=~s:0010:0011: ||$:=~s:0011:0100: ||$:=~s:0100:0101: ||$:=~s:0101:0000: &&( $~=~s:0000:0001: ||$~=~s:0001:0010: ||( $~=~s:0010:0011: ? $"=='0001' &&( $~=$" , $" ='0000' ):$~=~s:0011:0100: or$~=~s:0100:0101: or$~=~s:0101:0110: or$~=~s:0110:0111: or$~=~s:0111:1000: or$~=~s:1000:1001: )) ||$~=~s:1001:0000: &&$"=~s:0000:0001: ||$"=~s:0001:0000:; print "\r", $",'.',$~,':',$:,'.',$.,':',$^,'.',$_; redo }
    Replace line 2 with this block to use the current time
    @_=localtime; $~=$_[2]%12%10;$"=$_[2]%10>9; $.=$_[1]%10;$:=$_[1]-$.;$:/=10; $_=$_[0]%10;$^=$_[0]-$_;$^/=10; foreach $,($~,$",$.,$:,$_,$^){$, = sprintf"%04b",$,;}

    --
    I'm not belgian but I play one on TV. On dit que je parle comme un belge aussi.

Re: Binary Clock Implemented Through String Matching
by zentara (Cardinal) on Mar 11, 2005 at 12:17 UTC
    I'm not sure how this all works, but when I run it on linux, all I get is a repeating line:
    0000 0001:0000 0000:0000 01

    I'm not really a human, but I play one on earth. flash japh
Re: Binary Clock Implemented Through String Matching
by Vynce (Friar) on Mar 16, 2005 at 09:23 UTC
    i'm very curious why you named the program what you did -- my first thought was that it was the number of seconds until it looped or something, but that seems obviously not the case. enlightenment?