Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to localize an outside package variable through a lexical binding in Perl
    primarykey
    data
    text
    <p>It's a long title, but I'm afraid I can't take a single word out without losing the true meaning of the question. I'll give a quick description of what I'm trying to achieve first, then a long rambling on why I want it done this way. Skip the rambling if you intend to directly answer abiding with the question title :-)</p> <h2>Quick description</h2> <p>Assuming the existence of a <code>lexicalize</code> builtin that does just what I want, here goes:</p> <pre><code>package Whatever; { lexicalize $Other::Package::var; local $var = 42; Other::Package-&gt;do_stuff; # depends on that variable internally } </code></pre> <h2>Whys and whynots</h2> <p>For a bit of context, I'm whitebox-testing a third-party module.</p> <p>I want the variable <strong>localized</strong> because I want it changed for a limited time only, before the tests move on to something else. <code>local</code> is the best way I found to do this without depending on knowing the module's choice for an initial value.</p> <p>I want a <strong>lexical binding</strong> for two main reasons:</p> <ol> <li>I don't want to pollute the test file's higher-level namespace.</li> <li>I want something shorter than the fully-qualified name. It's not in the sample code for brevity, but I use the identifier a lot more than what's shown, with calculations and updates relative to its previous value.</li> </ol> <p>I couldn't decently use <code>our</code> because it won't grab a variable from another package: “No package name allowed for variable $Other::Package::var in "our".” Cheating to temporarily enter Other::Package's scope doesn't cut it: if I use a block (<code>{ package Other::Package; our $var }</code>) then the binding doesn't last long enough to be useful; and if I don't (<code>package Other::Package; our @var; package main</code>) then I need to know and copy the previous package's name, which prevents moving that piece of code around too much.</p> <p>While doing my homework before asking a previous form of this question, I discovered <code>Lexical::Var</code>, which seemed like it would be <em>exactly</em> what I needed. Alas: “Can't localize through a reference.”</p> <p>I've also tried my best on my gut feeling of <code>my *var</code>-based forms, but kept bumping into syntax errors. I've learned more than I cared to about scoping and binding today :-)</p> <p>I can't think of a reason why what I want shouldn't be possible, but I can't find the right incantation. Am I asking for an unfortunate unimplemented edge case?</p>
    singulars
    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