Config::Cfe - File configuration module
use Config::Cfe;
This module contains functions that ease updating small text files, usually konfiguration files. Will complain if the file is more than 100000 lines (configurable).
It is inspired from cfengine, but does only the editing, cfengine can do much more.
Boolean test functions
A few predefined boolean functions are available for easy architecture checking:
Function True if this is _________________________________________ is_debian a Debian GNU/Linux system, returns the actual version is_linux a Linux system is_i386 a 386-based system is_i486 a 486-based system is_i586 a 586-based system is_i686 a 686-based system is_sunos a SunOS system is_sunos4 a SunOS 4 system is_sunos5 a SunOS 5 system is_freebsd a FreeBSD system
These functions returns some interesting values about the OS.
Function Returns _________________________________ par_os_type OS type from 'uname' par_os_rev OS revision from 'uname', modified to integer par_mach_type OS architecture from 'uname'
Returns the current version of the cfe-module. If called with a version number, cfe will die if cfe is less than this version.
If debug is non-zero then write_file will write the new file but not rename the file from file.new to file. verbose greater than 0 will print out debug info.
Example: use Getopt::Std; getopt('dv'); $host = hostname; set_debug($opt_d, $opt_v);
Start editing a new file, will overwrite any existing file. A new file will get file mode 0666 & umask.
Start editing an existing file. If filename doesn't exist, read_file will either complain or, if optional second argument is true, create the file.
Write the current file if it has been changed. Control of the file can be done thru the hash parameter par. The old file will be renamed to .filename.cfe. Will not rename the final file (filename.new) to filename if debug is active.
Accepted keys for par:
key name default -------------------------------------- mode filemode 0666 & umask. uid numeric userid 0 gid numeric groupid 0
Quit editing the current file.
Appends filename at the current line.
This is a debug function, it will list all current lines to stdout.
get_line will return a pointer to an array with the lines from from_line up to, not including to_line, or the specified line if to_line isn't specified. The array is a copy of the real lines, any changes to this array will be lost.
Search for regexp, returns true at the first occurance. locate always starts at the beginning.
Increments the line pointer, negative values allowed. Returns the new line number.
Set the first line to be the current line.
Set the last line to be the current line.
Returns the current line number. Sets the current line number if arg N exist;
Returns the last line number.
Appends one or more lines after the current line. The line pointer will point to the last appended line.
Inserts one or more lines before the current line. The line pointer will point to the first inserted line.
Append a list after the current line. The result is one or more lines starting with prefix and the list joined by separator. If the line is too long it will be broken into several separated by newlinestring. Default for separator is ':' and for newlinestring '\\\n'. Default for length is 75. Example:
@list = qw(/bin /usr/bin /usr/local/bin); add_list PATH= \@list; will look like this: PATH=/bin:/usr/bin:/usr/local/bin
Deletes all lines containing /regexp/. Returns the deleted lines. If regexp is a pointer to a function or a anonymous function, it will be executed with current line number and a pointer to the current line as argument. The line will be deleted if the function returns true.
Deletes all lines from the current to the line matching regexp. Returns '' if regexp not found, otherwhise returns the deleted lines. Current line will be the line preceding the deleted area.
Deletes N lines from the current. Deletes to the end of file if N equal end. Returns the deleted lines; Current line will be the line preceding the deleted area.
end
Deletes all lines.
find_str returns true if any line matching regexp is found. Current line is not changed.
eval_to will evaluate exp on each line up to the line matching regexp. Returns true if any line was changed. exp can be any perl expression like 's/bin/usr/'. If exp is a pointer to a function or a anonymous function, it will be executed with current line number and a pointer to the current line as argument. Changes to the current line will be preserved.
's/bin/usr/'
eval_all will evaluate exp on each line in the file. Returns true if any line was changed. See eval_to.
eval_n will evaluate exp on N lines from the current line. Returns true if any line was changed. A value of 0 will change the current line, 1 will change the current and the next line. See eval_to. Evaluates to the end of file if N equal end.
eval_where will evaluate exp for each line matching regexp. Returns true if any line was changed. See eval_to.
replace_all replaces all occurrences of the string from with the string to.
set_comment sets the current comment strings. start_comment defaults to '# ', end_comment defaults to ''. Using set_comment whithout arguments will set the default comment strings again.
comment_where inserts a comment in the beginning of each line matching regexp. See also set_comment.
comment_to inserts a comment in the beginning of each line from the current line up to the line matching regexp. See also set_comment.
comment_n inserts a comment in the beginning of N lines starting with the current line. See also set_comment. Comments to the end of file if N equal end.
uncomment_where removes the comment in the beginning of each line matching regexp. See also set_comment.
uncomment_to removes the comment in the beginning of each line from the current line up to the line matching regexp. See also set_comment.
comment_n inserts a comment in the beginning of N lines starting with the current line. See also set_comment and eval_n. Uncomments to the end of file if N equal end.
Start appending a new section by adding a CFE tag named section.
Start inserting a new section
Appends a comment at current line that marks the end of the section.
Deletes the section name section. Returns true if section actually was deleted. Will set the current line.
Returns true if section exist. Will set the current line.
Comment out the section name section. Will set the current line.
Enable the section name section by removing the comment string at the beginning of the lines. Will set the current line.
Fetch the content of section named section, return a pointer to an anonymous array. Will set the current line.
To install Config::Cfe, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Config::Cfe
CPAN shell
perl -MCPAN -e shell install Config::Cfe
For more information on module installation, please visit the detailed CPAN module installation guide.