stringperl

perl: show raw string, including special characters


I want to print out the raw text of a string, without interpolating special characters. So, for example, if my string is "hello\nthere", I want to print that, rather than

hello
there

The string is stored in a variable, I'm not working with literals, so it's not just a matter of single-quote vs double-quote (I don't think). How can I do this?


Solution

  • The right incantation of Data::Dumper will do this.

    use Data::Dumper;
    $Data::Dumper::Terse = 1;
    $Data::Dumper::Useqq = 1;
    
    $foo = "hello
    there";
    print Dumper $foo;         #  "hello\nthere"