Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy would value passed through $m->comp() lose its value irregularly?
    primarykey
    data
    text
    <p>I have a perl mason component which is called to display an html page containing threaded comments. It uses Class DBI for loading from a MySQL database. </p> <p>The problem is that sometimes, <em>and I mean sometimes</em>, <em>very irregularly</em> a variable loses its value partway through the code. I am not changing the code between when it starts and stops happening, just reloading the page. It's not even that it just has the value on one page load and not the next. It's that on one page load you can print something to show that the variable contains a ref to an object (a "Person" with a name, etc.) and later on in the code on that same page load you can print that again and show that it doesn't. On the next page load the variable may retain its value all the way through. The only thing that is happening is that the variable gets passed through a call to $m->comp(), and a default gets applied if it is empty.</p> <p>Further, it happens for each and every comment, effectively losing its value many times in the same page load.</p> <p>Unfortunately (or fortunately, depending on how you look at it) I can't post all the code involved verbatim, but it boils down to the following, note the two commented lines marked "HERE":</p> <pre><code>&lt;%init&gt; my @comments = $dc-&gt;document-&gt;search("type = 'comment'"); &lt;/%init&gt; &lt;div&gt; &lt;&amp; '.comments', all_comments =&gt; \@comments &amp;&gt; &lt;/div&gt; &lt;%def .comments&gt; &lt;%args&gt; $all_comments &lt;/%args&gt; &lt;%init&gt; my @comments; @comments = @$all_comments; &lt;/%init&gt; % for my $c (@comments) { % my $poster = p($c-&gt;get_value('poster')); % $poster = Person-&gt;get_anonymous unless ref $poster; % # HERE: The variable is a ref to a given Person. % $m-&gt;comp('.comment', poster =&gt; $poster); % } &lt;/%def&gt; &lt;%def .comment&gt; &lt;%args&gt; $poster =&gt; Person-&gt;get_anonymous &lt;/%args&gt; &lt;%init&gt; # HERE: The variable is now the result of Person-&gt;get_anonymous instead. unless (ref $poster) { $poster = Person-&gt;get_anonymous; } &lt;/%init&gt; &lt;p&gt;&lt;% $poster-&gt;id == 1 ? ' (anonymous)' : $poster-&gt;fullname %&gt;&lt;/p&gt; &lt;/%def&gt; </code></pre> <p>I've tried removing that default, in which case I get an empty variable.</p> <p>This is a years old problem scrutinized down to these few lines of code where, programmatically speaking, something impossible seems to be happening. I'm left with a possible bug in Mason, or maybe some combination of things like some voodoo between Class DBI and the database losing a connection, or something I don't know about $m->comp().</p> <p>The only other clue I have is that when I've added something to the page, I've had the problem show up to an entirely new variable. Unfortunately, I never know when the problem is going to happen, and it happens seldom enough that I won't be able to go throw some suggested debugging code into it that will immediately give me something to report back. I'm only hoping for the off chance that somebody has experienced something similar or knows some possible problem that might explain what is going on.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload