how can I print multiple lines without escaping '$' and other characters?

Discussion in 'Perl Misc' started by bennett@peacefire.org, Sep 30, 2006.

  1. Guest

    Suppose I want to print multiple lines of output which contain dollar
    signs. The following will not work:

    print <<EOF ;
    A loaf of bread costs $1.
    A jug of milk costs $2.
    EOF

    because Perl will interpret the dollar signs. How can I print it
    without having to put a backslash in front of every $ sign?

    For example, I want to write a Perl script that prints out another Perl
    script as output, I don't want to have to write the second Perl script
    once using dollar signs and other special characters, then go back and
    insert backslashes in front of every special character.
    , Sep 30, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > Suppose I want to print multiple lines of output which contain dollar
    > signs. The following will not work:
    >
    > print <<EOF ;
    > A loaf of bread costs $1.
    > A jug of milk costs $2.
    > EOF
    >
    > because Perl will interpret the dollar signs. How can I print it
    > without having to put a backslash in front of every $ sign?


    Here docs are explained here.
    http://perldoc.perl.org/perlop.html

    Scroll down to 'Regexp Quote-Like Operators'

    Chris
    , Sep 30, 2006
    #2
    1. Advertising

  3. Ted Zlatanov Guest

    On 29 Sep 2006, wrote:

    > Suppose I want to print multiple lines of output which contain dollar
    > signs. The following will not work:
    >
    > print <<EOF ;
    > A loaf of bread costs $1.
    > A jug of milk costs $2.
    > EOF
    >
    > because Perl will interpret the dollar signs. How can I print it
    > without having to put a backslash in front of every $ sign?
    >
    > For example, I want to write a Perl script that prints out another Perl
    > script as output, I don't want to have to write the second Perl script
    > once using dollar signs and other special characters, then go back and
    > insert backslashes in front of every special character.


    In addition to single-quoting the "EOF" marker as others have
    suggested, you could use the __DATA__ marker:

    while (<DATA>) { read in the data }

    __DATA__
    This is my script full of $ signs

    I like this approach because it's very easy to open an external file
    later by just replacing the file handle name.

    Ted
    Ted Zlatanov, Oct 2, 2006
    #3
    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. Tamer Ibrahim
    Replies:
    3
    Views:
    401
    Alexey Smirnov
    Jan 29, 2007
  2. keto
    Replies:
    0
    Views:
    876
  3. jeffself
    Replies:
    5
    Views:
    5,833
    John Machin
    Apr 10, 2008
  4. Wolfgang
    Replies:
    1
    Views:
    145
    Paul Lalli
    Feb 13, 2004
  5. Cah Sableng
    Replies:
    0
    Views:
    225
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page