<div dir="ltr">gmail hiding &quot;reply to all&quot; often leads to me forgetting that I need to use that...<br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Tye McQueen</b> <span dir="ltr">&lt;<a href="mailto:tye.mcqueen@gmail.com">tye.mcqueen@gmail.com</a>&gt;</span><br>
Date: Mon, Aug 25, 2008 at 8:53 PM<br>Subject: Re: SPUG: Question -- use vs require<br>To: Trey Harris &lt;<a href="mailto:trey%2Bspug@lopsa.org">trey+spug@lopsa.org</a>&gt;<br><br><br><div dir="ltr"><div class="Ih2E3d">On Mon, Aug 25, 2008 at 12:47 PM, Trey Harris <span dir="ltr">&lt;<a href="mailto:trey%2Bspug@lopsa.org" target="_blank">trey+spug@lopsa.org</a>&gt;</span> wrote:<br>
</div><div class="gmail_quote"><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
&quot;The advantage&quot; makes it sound like you should be preferring it; you shouldn&#39;t. &nbsp;99%+ of the time, you want C&lt;use&gt;.<br>
</blockquote></div><div><br>Well, I think the &quot;99%+&quot; figure is overstated.&nbsp; &#39;require&#39; is perfectly appropriate in quite a few situations and is preferred in quite a few more-advanced situations.&nbsp; But, yes, if you are unsure which to use, &#39;use&#39; is the way to go.<br>

<br>It is rarely the case that replacing &#39;require&#39; with &#39;use&#39; will cause you problems (it can cause problems, but it /usually/ doesn&#39;t, especially for basic uses) and the reverse is not true (if you have code using &#39;use&#39;, it will often be because &#39;use&#39; is required and switching to &#39;require&#39; is likely to break things).<br>

&nbsp;</div><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">You can use C&lt;require&gt; in a string eval so the load happens at runtime rather than compile time</blockquote>

</div><div><br>No.&nbsp; Simply using &#39;require&#39; is enough to make the loading happen at run-time.&nbsp; You can also use &#39;use&#39; in a /string/ eval in order to get the loading and importing to happen at run-time but that also opens up lots of potential problems so it is a technique that should not be used lightly.<br>

&nbsp;</div><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">You can also use C&lt;require&gt; to prevent the C&lt;import&gt; method from being called (though ordinarily this isn&#39;t something you want to do, you should do a C&lt;use Module qw();&gt; instead to call C&lt;import&gt; with no arguments).</blockquote>

</div><div><br>Actually, qw() like that prevents import() from being called at all.&nbsp; To call import() with no arguments, write simply &#39;use Module;&#39;. And rather than just stating that &#39;require&#39; should be avoided, I&#39;ll provide reasons why one might or might not want to avoid it. :)<br>

<br></div><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
3. What are the implications for object-oriented programming?<br>
</blockquote>
<br></div>
That&#39;s a very broad question, but I don&#39;t think there are any implications of use vs. require for OOP versus any other type of programming.<font color="#888888"></font></blockquote></div><div><br>I would say that it is unusual for an OO module to export things so it is rare to have an OO module where &#39;require&#39; is not sufficient.&nbsp; So, for a /typical/ OO module, there is very little difference between &#39;use&#39;ing it vs. &#39;require&#39;ing it.<br>

<br>Even loading at compile-time vs. run-time makes little difference in a typical case with an OO module because the &#39;require&#39; is usually unconditionally executed near the very top of the requiring script/module.&nbsp; &quot;perl -c&quot; is one of the rare cases where you would see a difference (&quot;perl -c&quot; will check the syntax of any modules you &#39;use&#39;, but not of any modules you &#39;require&#39;).<br>

<br>If you have compile-time code below that makes use of the module, than you should certainly &#39;use&#39; the module.<br><br>Oh, let&#39;s not forget the &quot;obvious&quot; case: If you import symbols/behaviors from the module, then you need to &#39;use&#39; the module.<br>

<br>There are certainly plenty of advanced situations where &#39;require&#39; is a better choice than &#39;use&#39;.&nbsp; For the basic situations, the main reason I would use &#39;require&#39; is to make it clear that none of the syntax that follows is dependent on the module.<br>

<br>One only slightly advanced situations where &#39;require&#39; can be beneficial is when you have rarely-used part(s) of your code that are the only places where some heavy-weight module is needed.&nbsp; In those situations, it may be worth using &#39;require&#39; inside of the routine(s) that need the module so that the many runs that don&#39;t exercise those features won&#39;t need to waste time even loading the unutilized module.<br>

<br>Tye<br></div></div></div>
</div><br></div>