Array-Extract

 view release on metacpan or  search on metacpan

lib/Array/Extract/Example.pod  view on Meta::CPAN

exports a function C<extract> that does exactly what you might expect:

  my @done = extract { /\A\[[^ ]]/ } @todo;

C<@todo> is modified to remove anything that the block returns true for and
those elements are placed in C<@done>.

  open my $done_fh, ">>", $done_filename or die $!;
  my @done = extract { /\A\[[^ ]]/ } @todo;
  print { $done_fh } map { "$_\n" }
    "","Items archived @{[ DateTime->now ]}:","",@done;

=head2 Needs more actions

Of course, if all I wanted to do was remove the actions that had been
completed I probably wouldn't have reached for Tie::File, but for my next
trick I'm going to need to insert some extra content at the top of the file
once I'm done processing it.

I want to keep track of projects that have had all their remaining
actions marked as done and moved to the done file.  For example, I've ticked

lib/Array/Extract/Example.pod  view on Meta::CPAN


  #!/usr/bin/env perl

  use 5.012;
  use warnings;

  use Path::Class;
  use Tie::File;
  use Array::Extract qw(extract);
  use List::MoreUtils qw(uniq last_index);
  use DateTime;

  ########################################################################

  my $TODO = file($ENV{HOME}, "Dropbox", "SharedFolder", "TODO.txt");
  my $DONE = $TODO->dir->file("DONE.txt");

  ########################################################################

  # work out what projects are in this array, maintaining order
  sub projects(@) {

lib/Array/Extract/Example.pod  view on Meta::CPAN

  # work out what projects are in the file before we remove anything
  my @projects = projects @todo;

  # remove those items that are done
  my @done = extract { /\A\[[^ ]]/x } @todo;
  exit unless @done;

  # append what has been done to another file
  print { $DONE->open(">>") or die $! } map { "$_\n" }
    "",
    "Items archived @{[ DateTime->now ]}:",
    "",
    @done;

  # work out which projects no longer exist
  my %remaining_project = map { $_ => 1 } projects @todo;
  @projects = grep { !$remaining_project{ $_ } } @projects;

  # insert this at the section at the top of the file
  splice @todo,0,0,map { "$_ needs more actions" } @projects;



( run in 0.361 second using v1.01-cache-2.11-cpan-05444aca049 )