Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't even <em>refer</em> to a variable unless it's declared. When you ask </p> <pre><code>defined( $x ) ? </code></pre> <p>the compiler is going to complain: I don't know this what you're asking about, how am I supposed to tell it is defined? It has no point of reference for that variable, since you've indicated you do not want variables auto-created by name. </p> <p>If <code>strict 'vars'</code> was not on--which it is by default when you <code>use strict</code>--then it would create an entry in the package symbol table for 'x'. </p> <p>Interestingly enough is that without <code>strict 'refs'</code> it is also easy to check if a variable is in the package symbol table. </p> <pre><code>defined( *{ __PACKAGE__ . '::x' }{SCALAR} ) </code></pre> <p>Since there is no way to auto-create a lexical ("my variables"), there is also not a standard way to check to see if lexicals are declared. Lexical variables are stored in the "pad". But there is a module <a href="http://search.cpan.org/perldoc?PadWalker" rel="nofollow noreferrer"><code>PadWalker</code></a> that can help.</p> <p>In order to check the current level, you could get a hash of the pad, and then check whether or not it exists in the current pad. You could also loop back up through the stack (the integer argument works something like <a href="http://perldoc.perl.org/functions/caller.html" rel="nofollow noreferrer"><code>caller</code></a>) to find where the most recent x was.</p> <pre><code>my $h = peek_my (0); exists $h-&gt;{x}; </code></pre>
 

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