nysus has asked for the wisdom of the Perl Monks concerning the following question:

I was trying to figure out if it was possible to modify a string by changing the value of an interpolated scalar variable inside the string after the string is declared. The following bit of code doesn't work but illustrates what I'm trying to achieve:
my $variable = 3; my $string = "Variable: ${\$variable}\n"; print $string; #results in "Variable: 3" $variable = 4; print $string; #still results in "Variable: 3" but I'd like it to result in "Variable +: 4"

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon";
$nysus = $PM . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Dynamic interpolation in a string with a scalar variable?
by NetWallah (Canon) on Nov 30, 2015 at 22:30 UTC
    Try an anonymous sub:
    my $variable = 3; my $string = sub {"Variable: $variable\n"}; # Simplified from your cod +e print $string->(); #results in "Variable: 3" $variable = 4; print $string->(); # Prints: Variable: 4

            Our business is run on trust. We trust you will pay in advance.

Re: Dynamic interpolation in a string with a scalar variable?
by choroba (Cardinal) on Nov 30, 2015 at 21:03 UTC
    This is a common question. Use a templating system (see Template) or String::Interpolate.
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      OK, I wasn't sure if there was some way to do it that was native to Perl. I will just use a standard function to modify the string. Thanks.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon";
      $nysus = $PM . $MCF;
      Click here if you love Perl Monks

        some way to do it that was native to Perl.

        The "native" way: with a closure:

        my $variable = 3; my $string = sub { return "Variable: $variable\n"; }; print &$string; $variable = 4; print &$string;

        I assume here that by "native" you mean "without using a module". However, be advised that many modules are written using "native" pure perl and that modules are as much a part of the language as the print function.

Re: Dynamic interpolation in a string with a scalar variable? -- aka Delayed String Interpolation
by Discipulus (Canon) on Dec 01, 2015 at 08:59 UTC
    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.
Re: Dynamic interpolation in a string with a scalar variable?
by Laurent_R (Canon) on Dec 01, 2015 at 11:32 UTC
    A variation on some of solutions already suggested (also a closure, but not an anonymous one), shown under the Perl debugger:
    DB<1> $variable = 3; DB<2> sub string(){"Variable = $variable\n";} DB<3> print string; Variable = 3 DB<4> $variable = 4; DB<5> print string; Variable = 4
    It almost looks like what you're looking for, doesn't it?