In any quoted string ("", '', qq{}, q{}, etc.),
a double backslash must be interpolated as a single backslash, because a backslash is used to
escape the quoting character(s). This makes it possible to get the one character string \ and the two character string
\', with '\\\'' and '\\', respectively.
Here-docs are the exception to this. One can always find a terminator, possibly several characters long, that doesn't appear within the string itself. Thus, backslashes are free to be regular characters in a here-doc.
I don't think you're missing anything; this is just how strings work in Perl. | [reply] [d/l] [select] |
In any quoted string ("", '', qq{}, q{}, etc.), a double backslash must be interpolated as a single backslash, because a backslash
is used to escape the quoting character(s). This makes it
possible to get the one character string \ and the two
character string
\', with '\\\'' and '\\', respectively.
I don't see what's wrong with having '\' mean \ and having
'\'."'" or q<\'> mean \'. It's a little more typing, but personally
if I was the creator of Perl I would sacrifice the special
meaning of backslashes in single-quoted strings for more
normality...oh well, I'll have to be content with a source
filter or using here-docs. But...
Here-docs are the exception to this. One can always find a terminator, possibly several characters long, that doesn't appear
within the string itself. <
This is something I've been thinking about. What if I'm
quoting arbitrary user-supplied data in an eval()? I
wouldn't want to remove any occurances of the here-doc
terminator I was using -- that might upset users whose
data contains my terminator. Even if the terminator is
a long and rare word, there is still a chance of data/terminator
clashing -- disallowing the terminator in the data seems
very cargo-cult and wrong to me.
I guess the best here-doc terminator
in this case would be a randomly-generated one...if only
Perl had a qb// operator to quote an arbitrary number of
bytes. (__DATA__ doesn't do what I want, since it is only
one section. And if I want multiple sections, I need separators.
The same exact problem all over again. Oh wait -- I could
just store the number of bytes inside each section of the
DATA. But then I have an evil arbitrary limit on how much
data the user can supply. Unless I use BER encoded integers.
Too much work for me.)
| [reply] |
my $data = get_some_arbitrary_data();
$data =~ s/^( *)$/ $1/gm;
$eval = "<<'';\n$data\n\n";
$result = eval $eval;
$result =~ s/^ //;
print $result;
If you specify an empty terminator, then the here-doc ends at the next empty line. Just make sure there are no empty lines in the arbitrary data; perhaps by prepending a space to every empty line. I added a space to every line consisting only of spaces, so I could convert the data back after the eval.
| [reply] [d/l] |