Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by MrSnrub (Sexton)
on Nov 12, 2012 at 21:24 UTC ( #1003509=perlquestion: print w/ replies, xml ) Need Help??
MrSnrub has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I am trying to get print statements inside a function call inside a here-doc to work. Here is what I have so far:
#!/usr/bin/perl use strict; use warnings; print <<"TEXT"; This is a @{[ PrintHereDoc() ]} here-doc. TEXT print "END\n"; # subroutine sub PrintHereDoc { print "print statement inside a "; return 0; }

I want it to print:

This is a print statement inside a here-doc.

...but instead it is printing:

print statement inside a This is a 0 here-doc.
Why is the "print statement inside a" getting printed first? Also, is there a way I can disregard printing the return value of the subroutine?

Comment on Print statements inside a function call in a here-doc don't work
Select or Download Code
Re: Print statements inside a function call in a here-doc don't work
by 2teez (Priest) on Nov 12, 2012 at 21:30 UTC

    This works...

    #!/usr/bin/perl use strict; use warnings; print <<"TEXT"; This is a @{[ PrintHereDoc() ]} here-doc. TEXT print "END\n"; # subroutine sub PrintHereDoc { return "print statement inside a "; }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Print statements inside a function call in a here-doc don't work
by aitap (Deacon) on Nov 12, 2012 at 21:39 UTC
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

    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

Re: Print statements inside a function call in a here-doc don't work
by CountZero (Bishop) on Nov 12, 2012 at 22:28 UTC
    As you see, a 0 is printed at the place of the subroutine call. That is of course the return 0 from your subroutine.

    But the print in your subroutine happens before the print of the heredoc and hence it shows up --as expected-- before the heredoc. You are not printing into the heredoc, but directly to your standard output device!

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-12-25 14:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls