Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Calling a function from within a HERE doc?

by borkabrak (Novice)
on Aug 16, 2005 at 20:23 UTC ( #484240=perlquestion: print w/ replies, xml ) Need Help??
borkabrak has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I have a simple question for which I am having difficulty finding an answer. It is this:
How do you call a function from within a HERE doc?
Here is an example of what I mean:
#!/usr/bin/perl use strict; use warnings; ### sub datestamp(){ use POSIX; return strftime("%D %r",localtime); } my $var_day = datestamp(); ### print <<HERE; Variable interpolation works - Instead of the literal text seen here, this variable's value is printed. >>$var_day<< But how do you do "function" interpolation? The next line prints itself literally, instead of being substituted with the function's return value. Is there some other way of writing this that does what I want? >>&datestamp()<< HERE
I hope the example is sufficiently clear. I must admit that I hesitate to put forth a question that seems as if it should have such a simple and straightforward answer. However, I find myself unable to attain any such enlightenment on this issue, regardless of where I look. Though I sure the blame lies with my too modest research skills, any help will be most appreciated.
Thanks.

Comment on Calling a function from within a HERE doc?
Download Code
Re: Calling a function from within a HERE doc?
by Anonymous Monk on Aug 16, 2005 at 20:29 UTC
    You can do it.... with variable interpolation!
    print <<HERE; This is a string. Here's some more. The time is currently: @{[ scalar localtime() ]} HERE
    The @{} interpolates an array into your here-doc, and the inner [] creates an anonymous array, whose elements consist of whatever expression(s) you want to put between them.
Re: Calling a function from within a HERE doc?
by borisz (Canon) on Aug 16, 2005 at 20:31 UTC
    my $x = <<__H__; A test @{[ &test ]}! __H__ sub test { 'function' } print $x;
    Boris
Re: Calling a function from within a HERE doc?
by kwaping (Priest) on Aug 16, 2005 at 20:33 UTC
    Here's another way, based on your code:
    #!/usr/bin/perl use strict; use warnings; ### sub datestamp(){ use POSIX; return strftime("%D %r",localtime); } my $var_day = datestamp(); ### print <<HERE, datestamp(), <<SECOND; Variable interpolation works - Instead of the literal text seen here, this variable's value is printed. >>$var_day<< But how do you do "function" interpolation? The next line prints itself literally, instead of being substituted with the function's return value. Is there some other way of writing this that does what I want? HERE here's some more text... la la la SECOND print "done!";
Re: Calling a function from within a HERE doc?
by borkabrak (Novice) on Aug 16, 2005 at 20:53 UTC
    Wow! Thank you! These are some cool ideas that I (obviously) wouldn't have thought of immediately.
    These replies are most appreciated. I love clever solutions. :)

      While it presents a much steeper learning curve, Template-Toolkit is extremely clever and robust.

      use strict; use warnings; use Template; use POSIX; my $tt = Template->new; $tt->process(\*DATA, { var_day => strftime("%D %r",localtime)} ) or die $tt->error; __DATA__ [% USE day = date(format => '%D %r') -%] >>[% var_day %]<< >>[% day.format(time) %]<<

      Update: fixed funny tyepo -- s/learning curse/learning curve/

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re: Calling a function from within a HERE doc?
by bart (Canon) on Aug 17, 2005 at 19:06 UTC
    You can do this, as well as interpolation in more normal double-quoted strings, with the module Interpolation, originally written by dominus as a joke, currently maintained by Jenda.

    I don't like the import interface, I think you should just call a tie a tie, but you can just use tie with the module without any changes.

    use Interpolation; tie my %EVAL, Interpolation => 'eval'; # or: tie my %EVAL, Interpolation => sub { shift }; print <<"END"; The sum of 3 and 4 is $EVAL{3+4}. END
    (Be sure to have at least one more line under "END")

    The key to how this works is that an index to a hash or an array is always interpreted as perl code. Next, a tied hash is used to execute an associated function, instead of actually fetching a hash value out of somewhere.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2014-11-23 22:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls