Shlomi Fish avatar Shlomi Fish committed 7e7eb93

Remove boilerplate and add main module.

Comments (0)

Files changed (5)

App-Countdown/Build.PL

         'Test::More' => 0,
     },
     requires => {
-        #'ABC'              => 1.6,
-        #'Foo::Bar::Module' => 5.0401,
+        'Carp' => 0,
+        'IO::Handle' => 0,
+        'perl' => '5.008',
+        'POSIX' => 0,
+        'strict' => 0,
+        'Time::HiRes' => 0,
+        'warnings' => 0,
     },
     add_to_cleanup     => [ 'App-Countdown-*' ],
     create_makefile_pl => 'traditional',

App-Countdown/Changes

 Revision history for App-Countdown
 
-0.01    Date/time
+0.0.1   Tue 13 Nov 17:31:24 IST 2012
         First version, released on an unsuspecting world.
-

App-Countdown/README

 App-Countdown
 
-The README is used to introduce the module and provide instructions on
-how to install the module, any machine dependencies it may have (for
-example C compilers and installed libraries) and any other information
-that should be provided before the module is installed.
+This command line application implements "countdown", a command similar to
+the UNIX sleep command (see http://en.wikipedia.org/wiki/Sleep_%28Unix%29 ),
+only that it displays the number of seconds remaining before the timeout.
 
-A README file is required for CPAN modules since CPAN extracts the README
-file from a module distribution so that people browsing the archive
-can use it to get an idea of the module's uses. It is usually a good idea
-to provide version information here so that people can decide whether
-fixes for the module are worth downloading.
-
+It accepts a single command line argument of an integer with the number
+of seconds left.
 
 INSTALLATION
 

App-Countdown/lib/App/Countdown.pm

 package App::Countdown;
 
 use 5.008;
+
 use strict;
 use warnings FATAL => 'all';
 
+use Time::HiRes qw(sleep time);
+use POSIX qw();
+use IO::Handle;
+
+use Carp;
+
 =head1 NAME
 
-App::Countdown - The great new App::Countdown!
+App::Countdown - wait a certain amount of time while displaying the remainging
+time.
 
 =head1 VERSION
 
-Version 0.01
+Version 0.0.1
 
 =cut
 
-our $VERSION = '0.01';
+our $VERSION = '0.0.1';
 
 
 =head1 SYNOPSIS
 
-Quick summary of what the module does.
-
-Perhaps a little code snippet.
-
     use App::Countdown;
 
-    my $foo = App::Countdown->new();
-    ...
-
-=head1 EXPORT
-
-A list of functions that can be exported.  You can delete this section
-if you don't export anything, such as for a purely object-oriented module.
+    App::Countdown->new({ argv => [@ARGV] })->run();
 
 =head1 SUBROUTINES/METHODS
 
-=head2 function1
+=head2 new
+
+A constructor. Accepts the argv named arguments.
+
+=head2 run
+
+Runs the program.
 
 =cut
 
-sub function1 {
+sub new
+{
+    my $class = shift;
+
+    my $self = bless {}, $class;
+
+    $self->_init(@_);
+
+    return $self;
 }
 
-=head2 function2
+sub _delay
+{
+    my $self = shift;
 
-=cut
+    if (@_)
+    {
+        $self->{_delay} = shift;
+    }
 
-sub function2 {
+    return $self->{_delay};
 }
 
+sub _init
+{
+    my ($self, $args) = @_;
+
+    my $argv = [@{$args->{argv}}];
+
+    my $delay = shift(@$argv);
+
+    if (!defined $delay)
+    {
+        Carp::confess ("You should pass a number of seconds.");
+    }
+
+    if ($delay !~ /\A[1-9][0-9]+\z/)
+    {
+        Carp::confess ("Invalid delay. Must be a positive integer.");
+    }
+
+    $self->_delay($delay);
+
+    return;
+}
+
+sub run
+{
+    my ($self) = @_;
+
+    STDOUT->autoflush(1);
+
+    my $delay = $self->_delay;
+
+    my $start = time();
+    my $end = $start + $delay;
+
+    my $last_printed;
+    while ((my $t = time()) < $end)
+    {
+        my $new_to_print = POSIX::floor($end - $t);
+        if (!defined($last_printed) or $new_to_print != $last_printed)
+        {
+            $last_printed = $new_to_print;
+            print "Remaining $new_to_print/$delay", ' ' x 40, "\r";
+        }
+        sleep(0.1);
+    }
+
+    return;
+}
+
+1;
+
 =head1 AUTHOR
 
-Shlomi Fish, C<< <shlomif at cpan.org> >>
+Shlomi Fish, L<http://www.shlomifish.org/>, C<< <shlomif at cpan.org> >> .
 
 =head1 BUGS
 
 automatically be notified of progress on your bug as I make changes.
 
 
-
-
 =head1 SUPPORT
 
 You can find documentation for this module with the perldoc command.

App-Countdown/t/boilerplate.t

 #!perl -T
+
 use 5.008;
+
 use strict;
 use warnings FATAL => 'all';
-use Test::More;
 
-plan tests => 3;
+use Test::More tests => 3;
 
 sub not_in_file_ok {
     my ($filename, %regex) = @_;
     );
 }
 
-TODO: {
-  local $TODO = "Need to replace the boilerplate text";
+TODO:
+{
+    # local $TODO = "Need to replace the boilerplate text";
 
-  not_in_file_ok(README =>
-    "The README is used..."       => qr/The README is used/,
-    "'version information here'"  => qr/to provide version information/,
-  );
+    not_in_file_ok(README =>
+        "The README is used..."       => qr/The README is used/,
+        "'version information here'"  => qr/to provide version information/,
+    );
 
-  not_in_file_ok(Changes =>
-    "placeholder date/time"       => qr(Date/time)
-  );
+    not_in_file_ok(Changes =>
+        "placeholder date/time"       => qr(Date/time)
+    );
 
-  module_boilerplate_ok('lib/App/Countdown.pm');
+    module_boilerplate_ok('lib/App/Countdown.pm');
 
 
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.