0

How is one supposed to write this?

print <<EOF; if $x
bla
EOF

I think it is called postfix notation, and a here-doc. I get a syntax error.

OK, I guess I'll just use

print "bla" if $x;

which, yes, does what I want.

4
  • 2
    What do you want to achieve? That the here-doc is only printed when $x is true? Commented Feb 14 at 12:17
  • OK, I added my goal. Thanks. Commented Feb 14 at 12:21
  • 1
    What you'd write, depends on what you want to do. If that conditional is supposed to be an independent statement, then you put the semicolon there in the middle. Same way as you'd do in print "foo\n"; if (...) { ... }. If it's supposed to be a postfix conditional, then it's part of the same statement, and you don't put the semicolon in there. Same way as in print "foo\n" if .... There's nothing odd with the here-doc there, it's just a different-looking quote-like operator, same as '...' or qw/.../.
    – ilkkachu
    Commented Feb 14 at 12:26
  • Well some of us couldn't figure it out for days. That's why I am posting this Q&A set. To help others. Commented Feb 14 at 13:37

1 Answer 1

1

Use

print <<EOF if $x;
bla
EOF

See https://github.com/Perl/perl5/pull/21978 .

Not the answer you're looking for? Browse other questions tagged .