richard.sharpe has asked for the wisdom of the Perl Monks concerning the following question:

Dear spiritual brothers and sisters,

having found something, that transcend my understanding, and in the search for truth, I would like to share it with you. Having exactly this line in my here document, all $context_desc variables are interpolated correctly, except of the last one, which is interpolated as $context - I have such variable too.

action=setwpos %u +1 ($desc) ; shift ($context_desc) @{[COUNTER_START_VAR]} ; shift ($context_desc) @{[COUNTER_STATE_VAR]} ; eval @{[COUNTER_STATE_VAR]} ( \$counter=@{[COUNTER_STATE_VAR]}; ++\$counter; ) ; add ($context_desc) @{[COUNTER_START_VAR]} ; add ($context_desc) @{[COUNTER_STATE_VAR]} ; assign @{[COUNTER_STATE_VAR]} 0 ; set ($context_desc) $restart

Tried also ${context_desc}, but with the same result. I was suspitious, if this line is not too long for here document (not knowing, if there are some limitations), but this is not the longest line in here document, and I haven't found other failed interpolations. Tried also complete replacement of all characters in variable name (if hidding some invisible special character), but repeatedly the same result.

Any tips?

Using perl 5, version 26, subversion 1 (v5.26.1) built for x86_64-linux-gnu-thread-multi in WSL.

Replies are listed 'Best First'.
Re: inconsistent variable interpolation in here document
by davido (Cardinal) on Nov 28, 2019 at 22:09 UTC

    Could you do us the favor of creating a code example that demonstrates the behavior?

    I've taken your exact string and created variables and subroutines/constants as needed to provide a definition for everything the string tries to interpolate. Here is what I get:

    #!usr/bin/env perl use strict; use warnings; use Term::ANSIColor; sub colorize { my($color, $string) = @_; return color($color) . $string . color('reset'); } use constant COUNTER_STATE_VAR => colorize(bright_blue => '(COUNTER_ST +ATE_VAR)'); use constant COUNTER_START_VAR => colorize(bright_blue => '(COUNTER_ST +ART_VAR)'); my $desc = colorize('bright_green' => '|desc|'); my $context_desc = colorize('bright_green' => '|context_desc|'); my $restart = colorize('bright_green' => '|restart|'); my $var = <<"HERE"; action=setwpos %u +1 ($desc) ; shift ($context_desc) @{[COUNTER_START_ +VAR]} ; shift ($context_desc) @{[COUNTER_STATE_VAR]} ; eval @{[COUNTE +R_STATE_VAR]} ( \$counter=@{[COUNTER_STATE_VAR]}; ++\$counter; ) ; ad +d ($context_desc) @{[COUNTER_START_VAR]} ; add ($context_desc) @{[COU +NTER_STATE_VAR]} ; assign @{[COUNTER_STATE_VAR]} 0 ; set ($context_de +sc) $restart HERE print colorize(bright_red => "<<<<<\n") . $var . colorize(bright_red = +> ">>>>>\n");

    ...produces...

    <<<<< action=setwpos %u +1 (|desc|) ; shift (|context_desc|) (COUNTER_START_ +VAR) ; shift (|context_desc|) (COUNTER_STATE_VAR) ; eval (COUNTER_STA +TE_VAR) ( $counter=(COUNTER_STATE_VAR); ++$counter; ) ; add (|context +_desc|) (COUNTER_START_VAR) ; add (|context_desc|) (COUNTER_STATE_VAR +) ; assign (COUNTER_STATE_VAR) 0 ; set (|context_desc|) |restart| >>>>>

    Try running it. It looks better in a terminal that supports color; it's easier to see where the interpolation is happening. And then tell us where you were expecting to see interpolation but failing to get the behavior you would expect. If this snippet doesn't reproduce the error you are seeing, then your sample was inadequate in demonstrating the issue, and you'll need to provide us a self-contained snippet we can run to see it happening.


    Dave

      Thank you for your effort, Dave. I tried "snippetting" from my code base, but the output was consistent (the same as your snippet), when not in its own authentic context. It seemed weird to me, I was pretty sure, that I was looking on the right spot, but now I found out, that it wasn't true. Sorry for this distractions from my side, this was my fault, I had $context variable on other place, where $context_desc should be, and line I pasted, was not the right one. We can "close" (or completely delete, as it probably won't be useful for anybody) this node.

        PerlMonks really never deletes nodes. But not to worry; we all have asked questions that have been based on mistaken assumptions. It happens, nobody's judging (or they shouldn't be). And the thread still has value, as it demonstrates HERE doc interpolation, and a debugging technique.

        Truth be told, I expected that the result of boiling your bug down to a sample snippet would lead you to your answer. I thank you for having taken the time to do that, and am glad that it did help resolve the problem. Whenever I encounter something that makes me think Perl's not behaving as expected I try to practice that same strategy; write a one-liner, or a small (fewer than 25 lines) script that demonstrates the problem. And more often than not I won't even finish typing it before I have the ah-hah moment. It's tremendously unlikely that Perl's HERE docs have a bug in how they handle interpolation that is so glaring that one instance of interpolation would fail to happen. I don't know how many times I've thought to myself, "That shouldn't be happening, I'll pull it into an example snippet so I can play with it." And then presto, the issue turns up.


        Dave

Re: inconsistent variable interpolation in here document
by LanX (Saint) on Nov 28, 2019 at 20:54 UTC
    Hard to read, hence hard to tell

    > Any tips?

     @{[COUNTER_STATE_VAR]}

    @{[...]} is executing code, COUNTER_STATE_VAR could be a function with side effects.

    Otherwise please provide us with a short reproducible example and a print of the resulting string.

    Probably the problem is on the side of the receiver language not Perl.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      COUNTER_STATE_VAR is constant defined this way:

      use constant { # ... COUNTER_STATE_VAR => "%counter", # ... };

      Unfortunately, I am unable to reproduce with shorter example. I tried, but the output is consistent, when I am "snippetting" it out of its authentic context. I also cannot provide whole code base, because it contains a lot of information, which cannot be disclosed to third parties. Therefore I am seeking for tips, but the resolution is on me.