in reply to how to prevent new line added in HERE document

You effectivly embed your here-document into another here-document with results in an extra newline.

Take this code:

#!/usr/bin/perl -w use strict; use warnings; my $code=<<"END_MSG"; line 1 line2 END_MSG sub foo{ return $code; } print "before calling\n"; print $code; print "after calling\n"; print foo();
When you run this you'll see there is no extra newline - so calling a sub does not magically add one.

But what you do is to embed the $code argument in yet another here-document which is where your extra-newline comes from.