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

B

bennett

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

charley

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
 
T

Ted Zlatanov

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
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top