Search code examples
perlascii

How do i use \ (backslash) in a here-doc and have it show up when I print?


print <<EOTEXT;

      (`-') (`-')  _<-. (`-')_            <-. (`-')  
     _(OO ) ( OO).-/   \( OO) )     .->      \(OO )_ 
,--.(_/,-.\(,------.,--./ ,--/ (`-')----. ,--./  ,-.)
\   \ / (_/ |  .---'|   \ |  | ( OO).-.  '|   `.'   |
 \   /   / (|  '--. |  . '|  |)( _) | |  ||  |'.'|  |
_ \     /_) |  .--' |  |\    |  \|  |)|  ||  |   |  |
\-'\   /    |  `---.|  | \   |   '  '-'  '|  |   |  |
    `-'     `------'`--'  `--'    `-----' `--'   `--'

EOTEXT

This is my ascii art that id like to show up in console. How ever it seems that " \ " doesnt show up. Is there a way that i can make it appear.


Solution

  • In double-quoted string literals, \ is the start of an escape sequence. When followed by a non-word character, it causes that character to be produced. For example, \| and \␠ produce | and a space respectively. And of course, \\ produces \, so we can use \\ where we want \ in double-quote string literals.

    Here docs (<< string literals) act as double-quoted string literals, unless the token that follows the << is single-quoted. Then the string produced matches the input exactly.

    So we have the option of prepending \ to every special character (\, $ and @), or we can simply single-quote the token.

    print <<'EOTEXT';
    
          (`-') (`-')  _<-. (`-')_            <-. (`-')  
         _(OO ) ( OO).-/   \( OO) )     .->      \(OO )_ 
    ,--.(_/,-.\(,------.,--./ ,--/ (`-')----. ,--./  ,-.)
    \   \ / (_/ |  .---'|   \ |  | ( OO).-.  '|   `.'   |
     \   /   / (|  '--. |  . '|  |)( _) | |  ||  |'.'|  |
    _ \     /_) |  .--' |  |\    |  \|  |)|  ||  |   |  |
    \-'\   /    |  `---.|  | \   |   '  '-'  '|  |   |  |
        `-'     `------'`--'  `--'    `-----' `--'   `--'
    
    EOTEXT