In-place editing of YAML documents with Perl

I am new to Perl and YAML. I would like to read from the YAML file, as well as edit / write some property values ​​without overwriting the entire configuration file (preserving existing comments, empty lines, spaces, etc.).

I am using the YAML library in Perl. What would be a good way to achieve this?

+3
source share
1 answer

You cannot easily write part of a file - in the end, you will rewrite the entire file. If you write a partial file, you will need to search for the starting position, crop the file to this length (or crop, then search / add), and then write a new file tail after an unchanged start. File systems do not support operations such as "delete 329 bytes at offset 193 and insert 46 bytes after the resulting offset 227".

If your YAML module (library) saves or makes available leading comments and blank lines in some way, then you can easily save them. If not, then you probably have to do the work yourself - read and save the comment lines, then use YAML to parse the file, then write the saved comments and the replacement YAML.

+2
source

Source: https://habr.com/ru/post/1786393/


All Articles