Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>use</code> is equivalent to <code>BEGIN { require Module; Module-&gt;import( LIST ); }</code></p> <p>So, the main difference is that:</p> <ul> <li><p>Use is used at compile time</p></li> <li><p>Use automatically calls import subroutine (which can do <em>anything</em> but mostly used to export identifiers into caller's namespace)</p></li> <li><p>use dies if the module can not be loaded (missing/compile error)</p></li> </ul> <p><strong>As such:</strong></p> <ul> <li><p>When you need to load modules dynamically (for example, determine which module to load based on command line arguments), use <code>require</code>.</p></li> <li><p>In general, when you need to precisely control when a module is loaded, use <code>require</code> (<code>use</code> will load the module right after the preceding <code>use</code> or <code>BEGIN</code> block, <strong>at compile time</strong>).</p></li> <li><p>When you need to somehow bypass calling module's <code>import()</code> subroutine, use <code>require</code></p></li> <li><p>When you need to do something smart as far as handling load errors (missing module, module can't compile), you can wrap the <code>require</code> into an <code>eval { }</code> statement, so the whole program doesn't just die. </p> <p>You can simulate that with <code>use</code> but in rather in-elegant ways (trapping <code>die</code> signal in an early <code>BEGIN</code> block should work). But <code>eval { require }</code> is better.</p></li> <li><p>In all OTHER cases, use <code>use</code></p></li> </ul> <p>I didn't cover AUTOLOAD as that's a different beastie. Its usage is in cases when you want to intercpt calls to subroutines that you haven't imported into your namespace.</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