in reply to Record separator in variable

How you best solve this depends on how many escape sequences you want to support. For example, in this case a simple s/\\n/\n/g; s/\\r/\r/g; might be enough, but there are also modules such as String::Interpolate to give you the full power of Perl's interpolation (which may be too much power for the users of the configuration file). I would stay away from eval unless you are absolutely certain that you can always trust your input.

Replies are listed 'Best First'.
Re^2: Record separator in variable
by tecbuilt (Initiate) on Jun 06, 2019 at 17:53 UTC
    I contemplated doing the simple substitution but it's pretty specific to this example. Most customers should use either \n or \r\n for record separators but there is always the chance that one will have an ancient mainframe and send a file with something completely different. I'm looking at String::Interpolate right now, hopefully it will be useful.

      I wrote File::Edit::Portable for purposes such as this. It's quite flexible in reading and writing files with various record separators. It automatically determines the file's record separator, and the record separator of the platform the software is running on.

      Most of the distribution is Object Oriented based, but it has a couple of handy exports for the most basic functionality as well:

      use strict; use warnings; use File::Edit::Portable qw(recsep platform_recsep); $/ = recsep('file.txt'); # set the recsep to that of a file # or $/ = platform_recsep(); # set the recsep to that of the local platfor +m
      Your code must fully support your user documentation for this field and nothing more. Simple substitution may be a good idea. If you allow the full power of perl interpretation, you probably want the module that does that.
      Bill
Re^2: Record separator in variable
by daxim (Curate) on Jun 07, 2019 at 08:43 UTC