#!/usr/bin/perl -wl use strict; my %esc = ( '\0' => "\0", '\a' => "\a", '\b' => "\b", '\t' => "\t", '\n' => "\n", '\v' => "\013", '\f' => "\f", '\r' => "\r", ); my $literal = 'aaa\tbbb\nccc\tddd\t\\\\end'; (my $escaped = $literal) =~ s/(\\.)/exists $esc{$1} ? $esc{$1} : $ +1/eg; print $literal, "\n"; print $escaped; __END__ Prints: aaa\tbbb\nccc\tddd\t\\end aaa bbb ccc ddd \\end
Backslash has to be escaped in single quoted strings so it doesn't require a substitution.
The escape characters shown are the usual shell escapes. Perl adds a few more such as \e. Note the exists() isn't strictly required.
--
John.
In reply to Re: Substituting literal strings for escape characters
by jmcnamara
in thread Substituting literal strings for escape characters
by Tanalis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |