Escaping backslashes in 'HERE documents'?

Discussion in 'Perl Misc' started by Jim Ford, May 18, 2007.

  1. Jim Ford

    Jim Ford Guest

    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
     
    Jim Ford, May 18, 2007
    #1
    1. Advertisements

  2. The sentient life form Jim Ford posted the following:

    Try print <<'WEEKS';

    which should prevent interpolation.
     
    De Vliegende Hollander, May 18, 2007
    #2
    1. Advertisements

  3. Jim Ford

    Jim Ford Guest

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

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

    Jim Ford
     
    Jim Ford, May 18, 2007
    #3
  4. Jim Ford

    Raymundo Guest


    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;
     
    Raymundo, May 19, 2007
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.