Metronome: Difference between revisions
Added Perl example
SqrtNegInf (talk | contribs) (Added Perl example) |
|||
Line 1:
{{task}}<!--{{task|Temporal media}}-->
[[
The task is to implement a [https://en.wikipedia.org/wiki/Metronomemetronome metronome].
Line 697:
wait</lang>
=={{header|Perl}}==
The module <code>Time::HiRes</code> provides sub-second <tt>sleep</tt>. Text output only.
<lang perl>use Time::HiRes qw(sleep);
$beats_per_minute = shift || 72;
$beats_per_bar = shift || 4;
$duration = 60 / ($beats_per_minute * $beats_per_bar);
while () {
if (0 == $i++ % $beats_per_bar) {
$result .= "\nTICK";
} else {
$result .= " tick";
}
sleep $duration;
}</lang>
Sample run
{{out}}
<pre>$ metronome 60 6
TICK tick tick tick tick tick
TICK tick tick tick tick tick
TICK tick tick tick tick tick
TICK tick tick tick tick tick
^C
</pre>
=={{header|Perl 6}}==
|