Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you are mixing 'defined' and 'declared' concepts.</p> <p>Your are asking for 'How to check if variable is declared in perl' but then you are checking if a variable is defined. These are two different concepts.</p> <p>In perl if you use <em>'use strict'</em> you are automatically checking for any variable <strong>not declared</strong> (using <strong>my</strong>, <strong>local</strong> or <strong>our</strong>). Once you have a variable declared, you can test if it is defined (have a value assigned).</p> <p>So in your test, you are missing a prior declaration before testing for defineness</p> <pre><code>use strict; my $x; # you are missing this part [...] | # code # your test for define print defined $x? "defined\n" : "not defined\n"; </code></pre> <p>Please be aware the testing for only $x is incorrect for your purpose:</p> <pre><code>my ($x,$y, $z); $w; # not declared (use strict will catch it and die) $x = 0; # declared and defined BUT if you make a logic test like 'if ($x) {}' then it will be FALSE, so don't confuse testing for **'$x'** and testing for **'defined $x'** $y = undef; # declared but not defined $z = 1; # declared, defined, and logial test TRUE </code></pre> <p>Finally the answer of xenorraticide seems faulty to me: he suggest <strong>'unless $x' that is not correct</strong> for testing if defined as I said before. He also suggest <strong>'unless exists $x', that is wrong for testing scalars</strong>. 'exists' test is only for hashes keys (and deprecated for arrays).</p> <p>Hope this helps.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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