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

As LanX says, you could just chomp the parameter passed to sub foo.

chomp(my $code = shift);