View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Pavel Kuptsov > File-Operator-1.0.0 > File::Operator



Annotate this POD

View/Report Bugs
Module Version: 1.00   Source  


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


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


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.


 new        Method create File::Operator object and passing as arguments 
            the path to the file and filename to read/write/edit (or create).
                  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.


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 :)  

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);



P. A. Kuptsov,


File::LineEdit on CPAN

syntax highlighting: