Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Standard practice is to use <code>use</code> most of the time, <code>require</code> occasionally, and <code>do</code> rarely.</p> <p><code>do 'file'</code> will execute <code>file</code> as a Perl script. It's almost like calling <code>eval</code> on the contents of the file; if you <code>do</code> the same file multiple times (e.g. in a loop) it will be parsed and evaluated each time which is unlikely to be what you want. The difference between <code>do</code> and <code>eval</code> is that <code>do</code> can't see lexical variables in the enclosing scope, which makes it safer. <code>do</code> is occasionally useful for simple tasks like processing a configuration file that's written in the form of Perl code.</p> <p><code>require 'file'</code> is like <code>do 'file'</code> except that it will only parse any particular file one time and will raise an exception if something goes wrong. (e.g. the file can't be found, it contains a syntax error, etc.) The automatic error checking makes it a good replacement for <code>do 'file'</code> but it's still only suited for the same simple uses.</p> <p>The <code>do 'file'</code> and <code>require 'file'</code> forms are carryovers from days long past when the *.pl file extension meant "Perl Library." The modern way of reusing code in Perl is to organize it into modules. Calling something a "module" instead of a "library" is just semantics, but the words mean distinctly different things in Perl culture. A library is just a collection of subroutines; a module provides a namespace, making it far more suitable for reuse.</p> <p><code>use Module</code> is the normal way of using code from a module. Note that <code>Module</code> is the package name as a bareword and not a quoted string containing a file name. Perl handles the translation from a package name to a file name for you. <code>use</code> statements happen at compile time and throw an exception if they fail. This means that if a module your code depends on isn't available or fails to load the error will be apparent immediately. Additionally, <code>use</code> automatically calls the <code>import()</code> method of the module if it has one which can save you a little typing.</p> <p><code>require Module</code> is like <code>use Module</code> except that it happens at runtime and does not automatically call the module's <code>import()</code> method. Normally you want to use <code>use</code> to fail early and predictably, but sometimes <code>require</code> is better. For example, <code>require</code> can be used to delay the loading of large modules which are only occasionally required or to make a module optional. (i.e. use the module if it's available but fall back on something else or reduce functionality if it isn't.)</p> <p>Strictly speaking, the only difference between <code>require Module</code> and <code>require 'file'</code> is that the first form triggers the automatic translation from a package name like <code>Foo::Bar</code> to a file name like <code>Foo/Bar.pm</code> while the latter form expects a filename to start with. By convention, though, the first form is used for loading modules while the second form is used for loading libraries.</p>
    singulars
    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. 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