The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

File::Operator - Perl Object Oriented module for operation with text files

SYNOPSIS

  use File::Operator;
  my $fio=File::Operator->new(
                              -path => "/usr/home",
                              -file => "filename");
 # read methods
        %hash=$fio->FetchFileToHash();      
        $array=$fio->FetchRecord(-id => 123456789);
        $array2=$fio->FetchLastRecords(-num =>10,
                                        -raw =>1);
 # write/edit methods
        $write=$fio->WriteRecord(-id => 12346577,
                                 -record =>\@ARRAY);
        $fio=$fio->renew();
        $write=$fio->EditRecord(-id => 12346577,
                                -record =>\@ARRAY);
        $fio=$fio->renew();
        $write=$fio->DeleteRecord(-id => 12346577);
        $fio=$fio->renew();

DESCRIPTION

The module is intended for work with the simplified text database where data are stored in text files in the form of lines divided by a symbol |. The first field of record is called as an index which is unique key in a current file, and can be initialized by any value. The field of an index can be not transferred in methods, and to generate means of the module. The module allows to write, read, edit and delete records using their index, not caring about blocking files.

METHODS

 new        Method create File::Operator object and passing as arguments 
            the path to the file and filename to read/write/edit (or create).
            
            Usage:
                  my $fio=File::Operator->new(
                                              -path => "/home/foo",
                                              -file => "database.txt"
                                              );
            -path => Default value is current directory (e.q ".")
            
 renew      Method reopen filehandle after write/edit/delete operations and it need to
            call after write/edit methods.

READ METHODS

FetchFileToHash method returns a hash, containing all records in a file. Hash values is references on arrays. Hash keys is INDEXes. (the method is not recommended to be used with greater files).

Usage: %hash=$fio->FetchFileToHash();

      foreach my $gid(keys %hash) { 
      print "index: $gid $hash{$gid}->[0],$hash{$gid}->[1] ...\n"; }

FetchRecord method as parameter accepts INDEX. Returns the reference to a array corresponding transferred INDEX.

Usage: $array=$fio->FetchRecord(-id => INDEX); print "$array->[0] $array->[1]\n";

FetchLastRecords method returns the reference to a array. as parameter accepts quantity of the demanded records, (-num => NUMBER), and also unessential parameter -raw for return of not formatted data (with translation of lines and divided by a symbol |). The method is not recommended to be used with greater files.

Usage: # unformated output (raw output with | and \n symbols) $records=$fio->FetchLastRecords(-num =>10, -raw =>1); print "$records->[0]"; # formated output $records=$fio->FetchLastRecords(-num =>10);

       print "$records->[0]\n"; 

WriteRecord as parameters accepts -id => [unessential] and -record => \@ARRAYREF. In case of absence of a field-id - generates id by a call of function time (). In case of successful operation - returns an index of new record.

Usage: my @DATA=qw(Header 11/05/2005 Good! This_is_just_example);

       my $index=$fio->WriteRecord( -record=>\@DATA,
                                   #-id => $index);
       #rebuid object or renew filehandler or ...just do it :)  
       $fio=$fio->renew(); 
     

EditRecord as parameters accepts -id => INDEX [required here] and -record => \@ARRAYREF. In case of successful operation - returns 1 (e.q $result==1 in example).

Usage: my @DATA=qw(Edit 11/05/2005 Good! This_is_just_edit_example);

        my $result=$fio->EditRecord( -record=>\@DATA,
                                     -id => INDEX
                                       );
        $fio=$fio->renew();# just do it ;-)

DeleteRecord as parameters accepts -id => INDEX [required here] and returns 1 if successfull.

Usage: my $result=$fio->DeleteRecord( -id => INDEX);

        $fio=$fio->renew();         

AUTHOR

P. A. Kuptsov, ya@poizon.net.ru

SEE ALSO

File::LineEdit on CPAN