Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Perl, is it better to use a module than to require a file?
    text
    copied!<p><a href="https://stackoverflow.com/questions/2178871/is-there-anything-wrong-with-using-requires-after-output-starts-printing">Another question</a> got me thinking about different methods of code reuse: <code>use</code> vs. <code>require</code> vs. <code>do</code></p> <p>I see a lot of posts here where the question centers around the use of <code>require</code> to load and execute code. This seems to me to be an obvious bad practice, but I haven't found any good resources on the issue that I can point people to.</p> <p><a href="http://perldoc.perl.org/perlfaq8.html#What%27s-the-difference-between-require-and-use?" rel="noreferrer">perlfaq8</a> covers the difference between <code>use</code> and <code>require</code>, but it doesn't offer any advice in regard to preference (as of 5.10--in <a href="http://perldoc.perl.org/5.8.8/perlfaq8.html#What%27s-the-difference-between-require-and-use?" rel="noreferrer">5.8.8 there is a quick bit of advice in favor of use</a>).</p> <p>This topic seems to suffer from a lack of discussion. I have a few questions I'd like to see discussed:</p> <ol> <li>What is the preferred method of code reuse in Perl? <ul> <li><code>use ModuleName;</code></li> <li><code>require ModuleName;</code></li> <li><code>require 'file.pl';</code></li> <li><code>do 'file.pl';</code></li> </ul></li> <li>What is the difference between <code>require ModuleName</code> and <code>require "file.pl"</code>?</li> <li>Is it ever a good idea to use <code>require "file.pl"</code>? Why or why not?</li> </ol>
 

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