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. Advertising

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

    > print <<"WEEKS";



    Try print <<'WEEKS';

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

  3. Jim Ford

    Jim Ford Guest

    De Vliegende Hollander wrote:
    > The sentient life form Jim Ford posted the following:
    >
    >> print <<"WEEKS";

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

    Raymundo Guest

    On 5¿ù19ÀÏ, ¿ÀÀü6½Ã50ºÐ, Jim Ford <jaf...@watford53..freeserve.co.uk> wrote:
    > De Vliegende Hollander wrote:
    > > The sentient life form Jim Ford posted the following:

    >
    > >> print <<"WEEKS";

    >
    > > 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



    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    14,791
    Roedy Green
    Sep 5, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,364
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Replies:
    1
    Views:
    470
    Juan T. Llibre
    Oct 18, 2006
  4. Replies:
    4
    Views:
    547
    Flash Gordon
    Dec 11, 2005
  5. bilsch
    Replies:
    16
    Views:
    1,206
    Gene Wirchenko
    Jun 4, 2012
Loading...

Share This Page