Michael C. Davis wrote:
Is there a way to defer evaluation of the contents of a here-doc-defined value such that one can embed variables in the here-doc and not have them evaluated until they are used later? Something like this:
code: ----- use strict; use warnings;
my $header = <<'end_of_header'; # File: $filename end_of_header
my $filename = 'xyz'; print $header, "\n"; # output: want to see # File: xyz, but get # File: $filename
I tried a few variations and nothing seems to work, as shown below. (This RFC http://dev.perl.org/perl6/rfc/229.html from Perl 6 implies that there is fact no way to do this.) Can anyone clarify. Thank you.
i didn't check the link but from what you describe, i don't see any reason this can't be done:
#!/usr/bin/perl -w use strict;
my $s =<<'CODE'; I want to say: $v CODE
my $v = 'hello world';
print eval qq.qq,$s,.;
__END__
prints:
I want to say: hello world
david
To elaborate a bit, the reason for the failure is that while the string is interpolated, it is then also evaluated as perl code, so in
my $header = <<'end_of_header'; # File: $filename end_of_header
my $filename = 'xyz';
print eval $header;
the last statement ends up looking something like:
print # File: xyz;
which of course generates an error. The solution as David points out is to surround $header with double quotes. There are several ways to do this:
$header = '"' . $header . '"'; # double quote the string in $header print eval $header;
-or-
print eval "\"$header\"";
-or-
print eval "qq($header)";
-or-
print eval qq("$header");
etc.
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>