Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: inconsistent variable interpolation in here document

by davido (Cardinal)
on Nov 28, 2019 at 22:09 UTC ( #11109398=note: print w/replies, xml ) Need Help??

in reply to inconsistent variable interpolation in here document

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");


<<<<< 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.


Replies are listed 'Best First'.
Re^2: inconsistent variable interpolation in here document
by richard.sharpe (Sexton) on Nov 28, 2019 at 23:38 UTC

    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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11109398]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2020-05-26 13:44 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (150 votes). Check out past polls.