Escaping backslashes in 'HERE documents'?

J

Jim Ford

Is there a way of avoiding having to escape a backslash character with
another backslash in a 'HERE document'?

When creating Latex documents with a perl script, it's convenient to use
'HERE documents'. This enables Latex code to be written without the
clutter of 'print', double quotes, newline characters and semicolons -
producing almost 'clean' Latex coding. The only thing preventing 'pure'
Latex code being written is the necessity of escaping the ubiquitous
backslash character with another backslash - which ends up with this
sort of thing being created:

print <<"WEEKS";
\\begin{tabular}{|c||r|c|c|c|c|c|c|c|c|c|c|}
\\hline
& & {\\textbf{\\ 1\\ \\ }} &{\\textbf{\\ 2\\ \\ }} .... and so on


Jim Ford
 
J

Jim Ford

De said:
The sentient life form Jim Ford posted the following:



Try print <<'WEEKS';

which should prevent interpolation.

Thanks, but I need interpolation because I've got perl scalars in the
'Here document' e.g.:

\\textbf{$day} & \\textbf{Thursday} & & & & & & & & & & \\\\

Jim Ford
 
R

Raymundo

Thanks, but I need interpolation because I've got perl scalars in the
'Here document' e.g.:

\\textbf{$day} & \\textbf{Thursday} & & & & & & & & & & \\\\

Jim Ford


Hello,

I tried the following... It seems to work as what you want. But there
might be another problem. :)

#!/usr/bin/perl
$day = "2007.5.19";

# At first, use single quote and nothing is interpolated
$str=<<'EOF';
\textbf{$day} & \textbf{Thursday} & & & & & & & & & & \\
EOF

# Now replace single backslash with double backslashes
$str =~ s/\\/\\\\/g;
# Perform interpolation
eval '$str="'.$str.'";';
# Print
print $str;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top