Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Print statements inside a function call in a here-doc don't work

by davido (Archbishop)
on Nov 12, 2012 at 22:26 UTC ( #1003514=note: print w/replies, xml ) Need Help??

in reply to Print statements inside a function call in a here-doc don't work

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.


  • Comment on Re: Print statements inside a function call in a here-doc don't work

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003514]
[LanX]: pm != tk ;)
[Corion]: zentara: Most likely they used something like [http://www. node=123456] instead of [id://123456], which uses whatever domain you're visiting from
[LanX]: hmm... Actually this could be used for a phishing exploit ...
[LanX]: ... well like always :)
[zentara]: LanX One day, I will see a great interest in Tk. It's small, self-contained, needs very few external libraries, AND makes it easy to make high-contrast displays. :-)
[zentara]: Corion in this cased they left the www. out
[zentara]: /d// :-)
[LanX]: i like tk, just joking. but what about modern styles?

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2017-05-26 12:38 GMT
Find Nodes?
    Voting Booth?