karate_kid
karate_kid

Reputation: 155

print<<EOF in perl- To print $

Here I want to print the '$' sign. How to do that?

 #!/perl/bin/perl
  print <<EOF;
  This sign $ is called dollar
  It's a multiline
  string
  EOF

This is giving me result.

 This sign  is called dollar
 It's a multiline
 string

I want to print $.

Upvotes: 2

Views: 3638

Answers (2)

choroba
choroba

Reputation: 241878

Using EOF is equivalent to "EOF" - the here document is interpolated as if in double quotes. Backslash the dollar sign \$ or explicitly use single quotes to supress interpolation.

print << 'EOF';
...
EOF

Upvotes: 8

TLP
TLP

Reputation: 67910

Running your code with use warnings turned on gives me this:

Name "main::is" used only once: possible typo at foo.pl line 8.
Use of uninitialized value $is in concatenation (.) or string at foo.pl line 8.
This sign  called dollar
It's a multiline
string

As you can see, the is is gone from the sentence, and so is the dollar sign. The warnings tell me why: a variable $is was found inside the string. Since it was empty, it was replaced by the empty string. Because you did not have warnings turned on, this was done quietly.

The moral is: Always use use warnings. Also beneficial in this case would have been use strict, as it would have caused the script to fail compilation due to an undeclared variable $is.

As for how to fix it, I believe choroba has the solution in his answer.

Upvotes: 3

Related Questions