The simplest solution is to not print from within your subroutine. Instead, return the string that you want interpolated into the here doc. The "0" you are seeing is the return value from the sub. So just return what you want to appear in place of the "0".
The root of the problem is that you are misunderstanding the order of execution, which is dependant on when the string is available to be printed. For your first "print" statement to be executed, the entire target string must be known. For the entire target string to be known, Perl must perform the interpolation, which also means the subroutine must be executed. The subroutine must be executed completely before control is returned back to its caller. Part of the subroutine's code is to print something. Before Perl can obtain the return value of the subroutine the print statement within the subroutine must be executed. After that, the '0' is returned, and interpolated into your here doc, and the original print statement gets to finish up by printing the string that just got interpolated.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||