in reply to C style strings

I'm not sure how you define C-style string, do you have a proper specification?

Anyway, if your main worry is that Perl is doing interpolation, you could try to escape the $ and @ in the string

Though I have to admit I had problems with your full string so I shortened it...

This is only a stub, one would have probably also have to deal with already escaped $ and @ too.

use v5.12; use warnings; use Data::Dump qw/pp dd/; my $scl ="XXX"; my @arr = 1..3; # my $raw = <<'___'; # \r\n\t\tHello\tWorld\uFF00\\0\\n\\n\\!!\\"--->\\xf3\\"abc\\x4\\2\\8\ +\­2 # ___ my $raw = <<'___'; \r\n\t\tHello\tWorld $scl@arr ___ chomp $raw; dd $raw; $raw =~ s/(\$|\@)/\\$1/g; my $str = eval qq("$raw"); dd $str; # probably better done with unpack printf "%02X ",ord($_) for split //,$str;

"\\r\\n\\t\\tHello\\tWorld\n\$scl\@arr" "\r\n\t\tHello\tWorld\n\$scl\@arr" 0D 0A 09 09 48 65 6C 6C 6F 09 57 6F 72 6C 64 0A 24 73 63 6C 40 61 72 7 +2

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: C style strings
by harangzsolt33 (Deacon) on Dec 31, 2022 at 15:05 UTC
    I should have mentioned that I am trying to parse a C source code, and extract only the values. But the eval solution looks really clever! The only concern I have is what are the exact differences in C and Perl specification? Just the $ and the @ ? That's all? I was trying to go by the Wikipedia description of C escape sequences and how they work.
      > Just the $ and the @ ? That's all?

      I don't know, but it's unlikely.

      I suppose Perl augmented it's "escapism" over the versions.

      More importantly, what are the specifications for C and do they also depend on the version?

      For instance I had problems with the \u , and if it comes to Unicode inconsistencies then problems will be even manifold.

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

        Hmm... Yes, in TinyPerl 5.8, "\uFF00" becomes literal "FF00" instead of "\xFF\0", so eval isn't so clever after all. At least not in TinyPerl 5.8. :/
Re^2: C style strings
by karlgoethebier (Abbot) on Jan 01, 2023 at 12:55 UTC
    «…do you have a proper specification?»

    Since I had none at hand I took the here:

    "In C, the string data type is modeled on the idea of a formal string (Hopcroft 1979):

    Let Σ be a non-empty finite set of characters, called the alphabet.
    A string over Σ is any finite sequence of characters from Σ.
    For example, if Σ = {0, 1}, then 01011 is a string over Σ."

    Seacord, R. C. (2020) EFFECTIVE C An Introduction to Professional C Programming no starch press

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Well I was referring to Escape sequences in C, i.e. specifically in "C-style strings" °

      NB: the WP article doesn't list many references, other pages talk about variations depending on the compiler.

      Another point seems to be the distinction between various C versions like C99 which introduced \uXXXX ...

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

      °) not the mathematical definition of a byte vector.