You cannot currently. Parsing anything at the moment requires the entire string to exist in memory.
Having said that, if you know the maximum number of lines your template can expand, you can do something like:
my $max = 3;
This is not the fastest way to do this, but he would not need to read the entire file in memory.
source share