NAME

Config::Cfe - File configuration module

SYNOPSIS

use Config::Cfe;

DESCRIPTION

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.

is_sunos is_sunos4 is_sunos5 is_freebsd is_linux is_debian is_i386 is_i486 is_i586 is_i686

Boolean test functions

predefined 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

predefined parameter functions

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'

cfe_version [minimal version]

Returns the current version of the cfe-module. If called with a version number, cfe will die if cfe is less than this version.

set_debug [debug [verbose]]

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

new_file filename

Start editing a new file, will overwrite any existing file. A new file will get file mode 0666 & umask.

read_file filename [,1]

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_file [\%par]

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

abort_file

Quit editing the current file.

append_file filename

Appends filename at the current line.

list_lines [from_line [to_line]]

This is a debug function, it will list all current lines to stdout.

get_line from_line [to_line]

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.

locate regexp

Search for regexp, returns true at the first occurance. locate always starts at the beginning.

incr [[-]n]

Increments the line pointer, negative values allowed. Returns the new line number.

goto_top

Set the first line to be the current line.

goto_end

Set the last line to be the current line.

curr_line [N]

Returns the current line number. Sets the current line number if arg N exist;

last_line

Returns the last line number.

append line1 [line2 [...]]

Appends one or more lines after the current line. The line pointer will point to the last appended line.

insert line1 [line2 [...]]

Inserts one or more lines before the current line. The line pointer will point to the first inserted line.

add_list prefix listpointer [separator [newlinestring [suffix [length]]]]

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

delete_all regexp

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.

delete_to regexp

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.

delete_n N

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.

delete_file

Deletes all lines.

find_str regexp

find_str returns true if any line matching regexp is found. Current line is not changed.

eval_to regexp exp

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.

eval_all exp

eval_all will evaluate exp on each line in the file. Returns true if any line was changed. See eval_to.

eval_n N exp

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 regexp1 exp1 [regexp2 exp2 [...]]

eval_where will evaluate exp for each line matching regexp. Returns true if any line was changed. See eval_to.

replace_all from1 to1 [from2 to2 [...]]

replace_all replaces all occurrences of the string from with the string to.

set_comment [start_comment [end_comment]]

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 regexp

comment_where inserts a comment in the beginning of each line matching regexp. See also set_comment.

comment_to regexp

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 N

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 regexp

uncomment_where removes the comment in the beginning of each line matching regexp. See also set_comment.

uncomment_to regexp

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.

uncomment_n N

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.

append_sect section

Start appending a new section by adding a CFE tag named section.

insert_sect section

Start inserting a new section

end_sect

Appends a comment at current line that marks the end of the section.

delete_sect section

Deletes the section name section. Returns true if section actually was deleted. Will set the current line.

find_sect section

Returns true if section exist. Will set the current line.

comment_sect section

Comment out the section name section. Will set the current line.

uncomment_sect section

Enable the section name section by removing the comment string at the beginning of the lines. Will set the current line.

get_sect section

Fetch the content of section named section, return a pointer to an anonymous array. Will set the current line.