[BACK]Return to Todo.pm CVS log [TXT][DIR] Up to [local] / todotxt / Text-Todo / lib / Text

Annotation of todotxt/Text-Todo/lib/Text/Todo.pm, Revision 1.3

1.1       andrew      1: package Text::Todo;
                      2:
1.3     ! andrew      3: # $RedRiver: Todo.pm,v 1.2 2009/07/10 22:26:14 andrew Exp $
1.1       andrew      4:
                      5: use warnings;
                      6: use strict;
                      7: use Carp;
                      8:
1.2       andrew      9: use Class::Std::Utils;
                     10: use Text::Todo::Entry;
                     11:
1.1       andrew     12: use version; our $VERSION = qv('0.0.1');
                     13:
1.2       andrew     14: {
                     15:
                     16:     my %file_of;
                     17:     my %list_of;
1.1       andrew     18:
1.2       andrew     19:     sub new {
                     20:         my ( $class, $file ) = @_;
1.1       andrew     21:
1.2       andrew     22:         my $self = bless anon_scalar(), $class;
                     23:         my $ident = ident($self);
                     24:
1.3     ! andrew     25:         if ($file) { $self->load($file); }
1.2       andrew     26:
                     27:         return $self;
                     28:     }
                     29:
1.3     ! andrew     30:     sub file {
1.2       andrew     31:         my ( $self, $file ) = @_;
                     32:         my $ident = ident($self);
                     33:
                     34:         if ($file) {
                     35:             $file_of{$ident} = $file;
                     36:         }
                     37:
1.3     ! andrew     38:         return $file_of{$ident};
        !            39:     }
        !            40:
        !            41:     sub load {
        !            42:         my ( $self, $file ) = @_;
        !            43:         my $ident = ident($self);
        !            44:
        !            45:         $file = $self->file($file) || croak 'load requires a filename';
1.2       andrew     46:
                     47:         my @list;
                     48:         open my $fh, '<', $file or croak "Couldn't open [$file]: $!";
                     49:         while (<$fh>) {
                     50:             s/\r?\n$//xms;
                     51:             push @list, Text::Todo::Entry->new($_);
                     52:         }
                     53:         close $fh or croak "Couldn't close [$file]: $!";
                     54:         $list_of{$ident} = \@list;
                     55:
                     56:         return 1;
                     57:     }
                     58:
                     59:     sub save {
                     60:         my ( $self, $file ) = @_;
                     61:         my $ident = ident($self);
                     62:
1.3     ! andrew     63:         $file = $self->file($file) || croak 'save requires a filename';
1.2       andrew     64:
                     65:         open my $fh, '>', $file or croak "Couldn't open [$file]: $!";
                     66:         foreach my $e ( @{ $list_of{$ident} } ) {
1.3     ! andrew     67:             print {$fh} $e->text . "\n"
        !            68:                 or croak "Couldn't print to [$file]: $!";
1.2       andrew     69:         }
                     70:         close $fh or croak "Couldn't close [$file]: $!";
                     71:
                     72:         return 1;
                     73:     }
                     74:
                     75:     sub list {
1.3     ! andrew     76:         my ($self) = @_;
1.2       andrew     77:         my $ident = ident($self);
                     78:         return if !$list_of{$ident};
                     79:
                     80:         return $list_of{$ident};
                     81:
                     82:         #my $id = 1;
                     83:         #my @l;
                     84:         #foreach my $e ( @{ $list_of{$ident} } ) {
                     85:         #    push @l, $e; #{ %{$e}, id => $id };
                     86:         #    $id++;
                     87:         #}
                     88:         #
                     89:         #my @list = sort { $a->priority cmp $b->priority }
                     90:         #    grep { defined $_->priority } @l;
                     91:         #
                     92:         #push @list, grep { !defined $_->priority } @l;
                     93:         #
                     94:         #return \@list;
                     95:     }
1.1       andrew     96:
1.3     ! andrew     97:     sub add {
        !            98:         my ( $self, $entry ) = @_;
        !            99:         my $ident = ident($self);
        !           100:
        !           101:         if ( ref $entry ) {
        !           102:             if ( ref $entry ne 'Text::Todo::Entry' ) {
        !           103:                 croak(    'entry is a '
        !           104:                         . ref($entry)
        !           105:                         . ' not a Text::Todo::Entry!' );
        !           106:             }
        !           107:         }
        !           108:         else {
        !           109:             $entry = Text::Todo::Entry->new($entry);
        !           110:         }
        !           111:
        !           112:         push @{ $list_of{$ident} }, $entry;
        !           113:
        !           114:         return $entry;
        !           115:     }
1.2       andrew    116: }
1.1       andrew    117:
1.2       andrew    118: 1;    # Magic true value required at end of module
1.1       andrew    119: __END__
                    120:
                    121: =head1 NAME
                    122:
                    123: Text::Todo - [One line description of module's purpose here]
                    124:
                    125:
                    126: =head1 VERSION
                    127:
                    128: This document describes Text::Todo version 0.0.1
                    129:
                    130:
                    131: =head1 SYNOPSIS
                    132:
                    133:     use Text::Todo;
                    134:
                    135: =for author to fill in:
                    136:     Brief code example(s) here showing commonest usage(s).
                    137:     This section will be as far as many users bother reading
                    138:     so make it as educational and exeplary as possible.
                    139:
                    140:
                    141: =head1 DESCRIPTION
                    142:
                    143: =for author to fill in:
                    144:     Write a full description of the module and its features here.
                    145:     Use subsections (=head2, =head3) as appropriate.
                    146:
                    147:
                    148: =head1 INTERFACE
                    149:
                    150: =for author to fill in:
                    151:     Write a separate section listing the public components of the modules
                    152:     interface. These normally consist of either subroutines that may be
                    153:     exported, or methods that may be called on objects belonging to the
                    154:     classes provided by the module.
                    155:
1.2       andrew    156: =head2 new
                    157:
                    158: =head2 load
                    159:
                    160: =head2 save
                    161:
1.3     ! andrew    162: =head2 file
        !           163:
1.2       andrew    164: =head2 list
1.3     ! andrew    165:
        !           166: =head2 add
1.1       andrew    167:
                    168: =head1 DIAGNOSTICS
                    169:
                    170: =for author to fill in:
                    171:     List every single error and warning message that the module can
                    172:     generate (even the ones that will "never happen"), with a full
                    173:     explanation of each problem, one or more likely causes, and any
                    174:     suggested remedies.
                    175:
                    176: =over
                    177:
                    178: =item C<< Error message here, perhaps with %s placeholders >>
                    179:
                    180: [Description of error here]
                    181:
                    182: =item C<< Another error message here >>
                    183:
                    184: [Description of error here]
                    185:
                    186: [Et cetera, et cetera]
                    187:
                    188: =back
                    189:
                    190:
                    191: =head1 CONFIGURATION AND ENVIRONMENT
                    192:
                    193: =for author to fill in:
                    194:     A full explanation of any configuration system(s) used by the
                    195:     module, including the names and locations of any configuration
                    196:     files, and the meaning of any environment variables or properties
                    197:     that can be set. These descriptions must also include details of any
                    198:     configuration language used.
                    199:
                    200: Text::Todo requires no configuration files or environment variables.
                    201:
                    202:
                    203: =head1 DEPENDENCIES
                    204:
                    205: =for author to fill in:
                    206:     A list of all the other modules that this module relies upon,
                    207:     including any restrictions on versions, and an indication whether
                    208:     the module is part of the standard Perl distribution, part of the
                    209:     module's distribution, or must be installed separately. ]
                    210:
                    211: None.
                    212:
                    213:
                    214: =head1 INCOMPATIBILITIES
                    215:
                    216: =for author to fill in:
                    217:     A list of any modules that this module cannot be used in conjunction
                    218:     with. This may be due to name conflicts in the interface, or
                    219:     competition for system or program resources, or due to internal
                    220:     limitations of Perl (for example, many modules that use source code
                    221:     filters are mutually incompatible).
                    222:
                    223: None reported.
                    224:
                    225:
                    226: =head1 BUGS AND LIMITATIONS
                    227:
                    228: =for author to fill in:
                    229:     A list of known problems with the module, together with some
                    230:     indication Whether they are likely to be fixed in an upcoming
                    231:     release. Also a list of restrictions on the features the module
                    232:     does provide: data types that cannot be handled, performance issues
                    233:     and the circumstances in which they may arise, practical
                    234:     limitations on the size of data sets, special cases that are not
                    235:     (yet) handled, etc.
                    236:
                    237: No bugs have been reported.
                    238:
                    239: Please report any bugs or feature requests to
                    240: C<bug-text-todo@rt.cpan.org>, or through the web interface at
                    241: L<http://rt.cpan.org>.
                    242:
                    243:
                    244: =head1 AUTHOR
                    245:
                    246: Andrew Fresh  C<< <andrew@cpan.org> >>
                    247:
                    248:
                    249: =head1 LICENSE AND COPYRIGHT
                    250:
                    251: Copyright (c) 2009, Andrew Fresh C<< <andrew@cpan.org> >>. All rights reserved.
                    252:
                    253: This module is free software; you can redistribute it and/or
                    254: modify it under the same terms as Perl itself. See L<perlartistic>.
                    255:
                    256:
                    257: =head1 DISCLAIMER OF WARRANTY
                    258:
                    259: BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
                    260: FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
                    261: OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
                    262: PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
                    263: EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                    264: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
                    265: ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
                    266: YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
                    267: NECESSARY SERVICING, REPAIR, OR CORRECTION.
                    268:
                    269: IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
                    270: WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
                    271: REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
                    272: LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
                    273: OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
                    274: THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
                    275: RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
                    276: FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
                    277: SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
                    278: SUCH DAMAGES.

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>