Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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.


Dave


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003514]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-07-13 19:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (251 votes), past polls