in reply to Dynamic interpolation in a string with a scalar variable?

Hello nysus,

This is a frequent question as choroba said and was a question i made here sometime ago too; it happens when you try to understand how Perl works behind the courtains.

Anyway you must understand that you investigate such approach only for learning purpose, then in code you must use other tools like calling a subroutine or if the codebase is big, using templates Template::Toolkit

When i asked for Perl's wisdom at Interpolation: when it occurs? another beginner question.. i got indeed good replies and between them a genial one by tobyink that i propose here:
# tobyink's code http://www.perlmonks.org/?node_id=1020521 use v5.10; use strict; use warnings; sub delayed (&) { package delayed; use overload q[""] => sub { $_[0]->() }, fallback => 1; bless shift; } my $string = delayed {"Look $::person, no $::thing!"}; $::person = "Ma"; $::thing = "stringy eval";
tobyink was made so curoius by that question and he developped in one week his String::Interpolate::Delayed.
Be sure to read the docs for that module, specially the caveats part

HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
  • Comment on Re: Dynamic interpolation in a string with a scalar variable? -- aka Delayed String Interpolation
  • Download Code