gleeco has asked for the wisdom of the Perl Monks concerning the following question:

this is a Corpus Callosum biter-

# simple enough? $x = "\t";

..but how does one stringify the interpolated tab back to a backslash + 't' such that 'print $y' acutally prints '\t'?

Replies are listed 'Best First'.
Re: reverse interpolation
by parv (Parson) on Sep 17, 2008 at 06:31 UTC

    Is that a trick question? By using a look up table aka hash...

    my %map = ( "\t" => '\t' ); my $y = "\t"; print $map{ $y };

    See also Re^3: Generating characters (0 to 255).

    After skimming referenced post ... Removed unnecessary chr ord functions.

      Is that a trick question?

      Seeing as the title is "reverse interpolation", the OP probably wondered whether there is some way to have perl magically understand that if $foo = "bar", then print "bar" could actually print '$foo'...

      Seeing as it isn't, a more sensible approach emerged.

      Stop saying 'script'. Stop saying 'line-noise'.
      We have nothing to lose but our metaphors.

      so obvious i'm obviously not... but why the trouble with chr ord when it works without? thanks!

        You missed the update by a few seconds! You are right that chr ord chain was unnecessary (I started by finding the number to represent a tab, which I changed to use "ord", which changed to just the interpolated string).

        Obviously, I should have just started & ended with plain tab ... as you wrote "so obvious i'm obviously not". :}

Re: reverse interpolation
by moritz (Cardinal) on Sep 17, 2008 at 07:38 UTC
    You can use Data::Dumper and set $Data::Dumper::Useqq = 1 prior to dumping. No need to re-invent the wheel. (If that $VAR1 = disturbs you can set the Terse flag as well).
Re: reverse interpolation
by salva (Canon) on Sep 17, 2008 at 08:22 UTC
    use Scalar::Quote qw(quote); $x = "\t"; print quote($x), "\n";
Re: reverse interpolation
by repellent (Priest) on Sep 17, 2008 at 07:17 UTC
    Are you sure you don't want to print '\cI' instead of '\t'?
Re: reverse interpolation
by Anonymous Monk on Sep 17, 2008 at 07:10 UTC
    You could also do a replace:
    $y = $x; $y =~ s/\t/\\t/g;
Re: reverse interpolation
by Anonymous Monk on Sep 19, 2008 at 02:35 UTC
    http://perldoc.perl.org/functions/quotemeta.html
    $x = "\t"; print quotemeta $x;
      No, that doesn't return "\\t" as requested. It returns "\\\t"