NAME
Setup::Text::Snippet::WithID - Setup text snippet (with comment containing ID) in file
VERSION
version 0.08
SYNOPSIS
use Setup::Text::Snippet::WithID 'setup_snippet_with_id';
my $res = setup_snippet_with_id
file => '/etc/default/rsync',
id => 'enable',
content => 'RSYNC_ENABLE=1',
good_pattern => qr/^RSYNC_ENABLE\s*=\s*1/m,
replace_pattern => qr/^RSYNC_ENABLE\s*=.+/m;
die unless $res->[0] == 200;
Resulting /etc/default/rsync:
RSYNC_ENABLE=1 # SNIPPET id=enable
The above code's goal is to enable rsync daemon by default. If /etc/default/rsync already contains the "good pattern" (qr/^RSYNC_ENABLE\s*=\s*1/m), it will not be inserted with snippet. Snippet will replace text specified in replace_pattern (or if replace_pattern is not defined, snippet will be appended to the end of file [or beginning of file if top_style=>1]).
Example of multi-line snippet, in INI-style comment instead of shell-style:
; BEGIN SNIPPET id=default
register_globals=On
extension=mysql.so
extension=gd.so
memory_limit=256M
post_max_size=64M
upload_max_filesize=64M
browscap=/c/share/php/browscap.ini
allow_url_fopen=0
; END SNIPPET id=default
DESCRIPTION
This module uses Log::Any logging framework.
This module has Rinci metadata.
BUGS/TODOS/LIMITATIONS
If a snippet is removed due to should_exist=>0, its position is not recorded. Thus the undo step will reinsert snippet according to replace_pattern/top_style instead of the original position.
The undo also currently doesn't record whether newline was autoappended on the file, so it doesn't restore that.
TODO: Restore attrs.
SEE ALSO
AUTHOR
Steven Haryanto <stevenharyanto@gmail.com>
COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Steven Haryanto.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.