in reply to Why is this string only PARTIALLY interpolated?

The first string literal uses single quotes. These don't interpolate.

The second string contains $_ and $y. Both were interpolated.


$x was read in from mysql and the value in it is not interpolated.

What I hear is that you stored incomplete Perl code (partial string literals) in the database as a template.

Some earlier posts show how to attempt to fix the incomplete Perl code (by adding double quotes) and how to execute that code (by using eval EXPR). But this isn't a good idea.

The better solution is to use a proper template system!

Replies are listed 'Best First'.
Re^2: Why is this string only PARTIALLY interpolated?
by misterperl (Friar) on Aug 04, 2023 at 12:52 UTC
    Can you help me understand what you mean by "a proper template system" and how to implement it in MYSQL? I know what a template is, not sure I'm familiar with the set of "proper" ones. TY
      #!/usr/bin/env perl use strict; use warnings; use Template; my $from_db = q/Oh this year I'm off to Sunny [% country %]! /; my $tmpl = Template->new; for my $dest (qw/Greece Italy Spain/) { $tmpl->process (\$from_db, { country => $dest }); }

      $from_db is the immutable thing (the template) which you store in and retrieve from the DB. We then use Template to perform the required modifications. Template is one of the "proper" ones as you can find it listed in Task::Kensho. There are others, of course.


      🦛

        interesting. TY I did not know about template TY for the advice and education.

      You're processing the template in Perl, not MySQL. Template-Toolkit is my go-to template system. The template would look like

      [% cat.dog %]/mouse
      instead of
      $cat->{dog}/mouse
        interesting TY, studying this solution. I've never heard of this before (after LAMP programming since 1996!) or heard it ever mentioned in any of myriad OSCONs I've attended. A nice surprise!