http://www.perlmonks.org?node_id=1003509

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?

Replies are listed 'Best First'.
Re: Print statements inside a function call in a here-doc don't work
by davido (Cardinal) 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 2teez (Vicar) 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 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
Re: Print statements inside a function call in a here-doc don't work
by aitap (Curate) on Nov 12, 2012 at 21:39 UTC