in reply to Can't get \n or other character/translation escapes to interpolate if originally read from a data file

++AnomalousMonk explained the "why".

Here's something you can do to get the string in the format I think you want: the String::Interpolate module allows you to pass a string with character escapes and get back the string that I believe you desire.

C:\usr\local\share>perl use strict; use warnings; use v5.10; use String::Interpolate qw/interpolate/; my @lines = <DATA>; for my $line ( @lines ) { chomp $line; say "data line is *$line*"; my $real = interpolate($line); say "real line is *$real*"; } __DATA__ hello\ngoodbye good\nevil yes\nno
yields
data line is *hello\ngoodbye* real line is *hello goodbye* data line is *good\nevil* real line is *good evil* data line is *yes\nno* real line is *yes no*

note: I've never used it before today; I found it via a quick search, and its description and above behavior seemed right.

Also, I don't have the Programming Perl to check page numbers, but "translation escapes" sound like escapes that are only available to the regex substitution engine and not to normal strings, so those will likely not be available even in String::Interpolate (unchecked).

  • Comment on Re: Can't get \n or other character/translation escapes to interpolate if originally read from a data file
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Can't get \n or other character/translation escapes to interpolate if originally read from a data file
by haukex (Bishop) on Mar 15, 2021 at 17:35 UTC
    String::Interpolate

    However, it'll also interpolate @{[ `rm -rf /` ]}... depending on how many characters OP wants interpolated, s/\\n/\n/g might be the safer approach.

      Like I said, I had never used the String::Interpolate module before. Because they had the safe versions of their functions, I thought that they protected against things like that... but it appears not.

      If the OP wants more than just \n available as escapes in his processing, however, the list of substitutions will creep up as the rules/desires change... especially with the translation escapes. I was hoping that String::Interpolate was a way to avoid re-inventing that wheel, but I guess not.

        Because they had the safe versions of their functions, I thought that they protected against things like that... but it appears not.

        Safe can in theory protect against some attack vectors. But IMHO the major issue with that module is that its protection depends on blocking/allowing specific opcodes, which can change depending on Perl version, and AFAIK even which opcodes do what can change across Perl versions. So it requires deep knowledge of the internals, and even then it can't promise complete safety. Even though being able to eval Perl code "really safely" would be a nice thing to have, I would always strongly recommend against relying on Safe...

Re^2: Can't get \n or other character/translation escapes to interpolate if originally read from a data file (updated)
by AnomalousMonk (Bishop) on Mar 15, 2021 at 17:53 UTC
    ... "translation escapes" sound like escapes that are only available to the regex substitution engine and not to normal strings ...

    The \u \U \l \L \Q \E "translation escapes" are also available to the double-quote operator:

    Win8 Strawberry 5.8.9.5 (32) Mon 03/15/2021 12:41:31 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my $foo = "%^&*("; my $bar = 'Bar'; my $string = "\U$bar\E \l\U$bar\E \Q$foo\E \n"; print $string; ^Z BAR bAR \%\^\&\*\(

    Update: In fact, I think the only reason translation escapes are available to regex expressions is that double-quotish interpolation is done very early in compilation of a regex expression. IIUC, the first two steps are:

    1. Find the end of the expression;
    2. Do double-quotish interpolation.


    Give a man a fish:  <%-{-{-{-<

      "translation escapes" are also available to the double-quote operator:

      Cool. Learned something new today. :-)