The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

File::Reader - Perl extension for Read and write easily text file

SYNOPSIS

        use File::Reader qw( Write Read ReadConf ReWrite );
        @myArray = Read('/home/arnaud/.bashrc');
        @mysArray = sRead('/home/arnaud/.bashrc',1,2);
        $file = '/home/arnaud/write_by_File_Reader ;
        $message = "Something to write\n" ;
        Write($file, $message) ;
        @message=("something", "to", "write", "\n");
        Write($file, @message) ;
        sWrite($file,"Something to write\n",1,2) ;
        %conf_file = ReadConf('/etc/file.conf') ;

DESCRIPTION

Write :

        Write(file_to_write, data_to_write) : this function write some data in a file. You can call it like that : Write($file, @data) ;

Read :

        Read("file_to_read") : read all data in "file_to_read". B<IMPORTANT> file is read just as it is ! Don't forget to treat data incomming :-). Read() return B<undef> if the file that you try to open does not exist.

ReWrite :

        ReWrite(file_to_write, data_to_write) : write data in the end of "file_to_write". If file does not exist ReWrite() create him. The function don't erase the original file. To really re-write a file (erase and write it) use Write().

ReadConf :

        ReadConf("some_configuration_file") : Read a configuration file (wich is write like : key = value) and return a hash (usable by : $conf{key}). ReadConf() return B<undef> if the file that you try to read does not exist.

ReadConf2 :

        ReadConf2("some_configuration_file","separator") : same as ReadConf() but you can specify the separator to use in the configuration file.


        configuration file :
        
                key1 :: value1
                key2 :: value2
        
        usable with the code :
        
                %conf = ReadConf2("configuration_file",'::') ;
                print "$conf{key1}\n" ;

ReadConfAdv :

        ReadConfAdv($conf_fln,$conf_start,$conf_stop) : with this function you can use only one configuration file for several applications by putting separating beacons of section.

        Ex : %conf = ReadConfAdv("conf_file",'<start-tag>','<stop-tag>') ;

        work with a configuration file like :

                <start-tag>
                key1 = value1
                key2 = value2
                <stop-tag>
                [other-start]
                key3 = value3
                [other-stop]

ReadConfAdv2 :

        ReadConfAdv2($conf_fln,$conf_start,$conf_stop,$level,$sep) : same options that ReadConfAdv() but you can specify, moreover, the separator of the pairs of keys/values in the configuration file (like ReadConf2() ).

listDir :

        listDir("any_directory/" : return a table containing the contents of "any_directory/"

        Ex : @dir = listDir("/etc/") ;
  • The following function (s*) are the "secure" version of previous functions. They make somes tests on files before reading and writing them.

sReWrite :

        sReWrite($name,$data,$warning) : if you set $warning to 1 (default) File::Reader print test on STDERR. If $warning = 0, STDERR is redirected to file_reader_err.log.

sWrite :

        sWrite($name,$data,$warning) : same as sReWrite() but with Write() functionality.

sRead :

        sRead($name,$warning,$level) : see sReWrite() for explanations relating to $warning. $level can have several values :

                0 : (welcome hackers !) No real security on the $name variable
                1 : (Medium security) All escape shell characters are escaped (&|; etc.)
                2 : (paranoid) All escape shell caracters are deleted !
        If you use File::Reader in CGI it's strongly recommended to use the lvl 2.
        Default is 1

sReadConf :

        sReadConf($conf_fln,$level) : see above for explanations relating to $level

sReadConfAdv :

        sReadConfAdv($conf_fln,$conf_start,$conf_stop,$level) : see above for explanations relating to $level, $conf_start and $conf_stop

sReadConf2

        sReadConf2($conf_fln,$level,$sep) : see above for explanations relating to $level and $sep

sReadConfAdv2

        sReadConfAdv2($conf_fln,$conf_start,$conf_stop,$level,$sep) : see above for explanations relating to $conf_start, $conf_stop, $level and $sep

generateConfFile

        generateConfFile("perl_source","conf_file_name") : Generate a configuration file (named "conf_file_name") for "perl_source". 
        Moreover it re-write "perl_source" to support the new configuration file. 
        The original source is not changed. A new file is created, named "perl_source_withconf".
        The modified variables are those in upper case (as $VERSION).

        Ex :  Perl source file (donothing.pl) :

                #!/usr/bin/perl -w
                $VERSION = 1.0 ;
                $ETC_DIR = '/etc/' ;
                $var_dir = '/var/' ;
                print "my Version -> $VERSION, my etc directory -> $ETC_DIR and my var directory -> $var_dir\n" ;

        Now in another Perl script (gen_conf.pl wich usable in this tarball) :
                #!/usr/bin/perl -w
                use File::Reader qw( generateConfFile ) ;
                generateConfFile("donothing.pl","donothing.conf") ;

        And after execution (it may takesome time if the "perl_source" is important) you could see 2 new file in the directory : donothing.conf and do nothing_withconf.pl.

        donothing_withconf.pl source code :

                #!/usr/bin/perl -w
                use File::Reader qw (ReadConf ) ;
                my %conf = ReadConf("donothing.conf");
                $VERSION = $conf{VERSION} ;
                $ETC_DIR = '$conf{ETC_DIR};
                $var_dir = '/var/' ;
                print "my Version -> $VERSION, my etc directory -> $ETC_DIR and my var directory -> $var_dir\n" ;

        And donothing.conf :

                ## Configuration file for donothing.pl generated by File::Reader version 0.6
                VERSION = 1.0
                ETC_DIR = '/etc/'

EXPORT

None by default.

use File::Reader qw ( sWrite sReadConf ) ; # Export the two functions sWrite() and sReadConf().

COMPATIBILITY

You can assume the compatibility with the old syntax of Battosai by :

use File::Reader qw ( ecrire ouvre reecrire lireConfAdv lireConf listRep sReecrire sOuvre sLireConf sLireConfAdv lireConfAdv2 sLireConf2 sLireConfAdv2 lireConf2 ) ;

But this method is deprecated. Moreover new function will never profit from binding for old syntax.

The old syntax is no longer support.

AUTHOR

Arnaud DUPUIS, <arno@asocial.org>

COPYRIGHT AND LICENCE

Copyright (c) 2004 Arnaud DUPUIS <a.dupuis@infinityperl.org>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

perl