Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>my</strong> restrict the variables access to the innermost block in which they were declared. If there is no block, they are file-scoped.</p> <p><strong>our</strong> instead associates a simple name with a package variable in the current package , so it is declared at the package level and linked to the package name. <strong>our</strong> tries to help out by letting you use package variables without adding the package name.</p> <pre><code>package pack; our $variable; # These are the same $pack::variable; # These are the same </code></pre> <p>An <strong>our</strong> variable is something similar to C's static variable, but is different because the variable declared with <strong>our</strong> in a function is still accessible outside the function if it is called with the variable's fully qualified name.</p> <p>But most of all <strong>my</strong> is lexically scoped while <strong>our</strong> is lexical scope but their life persistent even outside the declaring block(their life is like global variable life), therefore to really understand the difference between <strong>my</strong> and <strong>our</strong> you have to understand the difference between lexically and global scoped in Perl.</p> <p>So briefly the difference between the two type are : </p> <h2>Global variables</h2> <p>Any code, anywhere, can change their values. </p> <h2>Lexical variables</h2> <p>The life of the variable end with the the end of the code block in with they are included, after that their values are garbage collected. These kind of variables can be accessed only within the block in which they are declared.</p> <hr> <p><strong>To answer you specific example-question :</strong> <em>try to move the second package declaration (package two) into another file, and you will see the difference between <strong>my</strong> and <strong>our</strong> ...</em></p>
 

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