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.


In reply to Binary Clock Implemented Through String Matching by ktross

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.