D
dede
Hello friends of the community,
find below the solution I am currently using to load external
text-files that
are seamlessly interpolated to behave like the useful HERE-docs:
echo <<"EOT";
Hello $strWorld ...
EOT
SNIPPET:
while (<$handle>) {
$_ =~ s/(\$\S+)/defined eval($1) ? eval($1) : $1/gsxe;
#interpolate all $vars or $self->vars etc.
#note that this "old test"...
#$_ =~ s/(\$\w+)/defined $1 ? $1 : "undefined"/gsxee;
#produced the notorious "undefined substitution errors"
#here might be some extra cleanup-work of the file
push @lines, $_;
} #each line;
close $handle;
A.
Note that you can substitute ANY var or OO-pseudo hash-var (ie.
inst-var)
from the namespace of the context (function/method) that this snippet
resides in.
B.
You might also be more specific and include some static text in the
search
pattern and then add some .. eval('whatever'.$1.'foo') .. stuff.
C.
Note that the defined eval (...) is necessary in order to return values
that
Perls likes to interpret as FALSE (eg. '' or 0).
Any comments / immprovements are welcome.
Share and Enjoy
dede, Paris / France
find below the solution I am currently using to load external
text-files that
are seamlessly interpolated to behave like the useful HERE-docs:
echo <<"EOT";
Hello $strWorld ...
EOT
SNIPPET:
while (<$handle>) {
$_ =~ s/(\$\S+)/defined eval($1) ? eval($1) : $1/gsxe;
#interpolate all $vars or $self->vars etc.
#note that this "old test"...
#$_ =~ s/(\$\w+)/defined $1 ? $1 : "undefined"/gsxee;
#produced the notorious "undefined substitution errors"
#here might be some extra cleanup-work of the file
push @lines, $_;
} #each line;
close $handle;
A.
Note that you can substitute ANY var or OO-pseudo hash-var (ie.
inst-var)
from the namespace of the context (function/method) that this snippet
resides in.
B.
You might also be more specific and include some static text in the
search
pattern and then add some .. eval('whatever'.$1.'foo') .. stuff.
C.
Note that the defined eval (...) is necessary in order to return values
that
Perls likes to interpret as FALSE (eg. '' or 0).
Any comments / immprovements are welcome.
Share and Enjoy
dede, Paris / France