<div dir="ltr">gmail hiding "reply to all" 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"><<a href="mailto:tye.mcqueen@gmail.com">tye.mcqueen@gmail.com</a>></span><br>
Date: Mon, Aug 25, 2008 at 8:53 PM<br>Subject: Re: SPUG: Question -- use vs require<br>To: Trey Harris <<a href="mailto:trey%2Bspug@lopsa.org">trey+spug@lopsa.org</a>><br><br><br><div dir="ltr"><div class="Ih2E3d">On Mon, Aug 25, 2008 at 12:47 PM, Trey Harris <span dir="ltr"><<a href="mailto:trey%2Bspug@lopsa.org" target="_blank">trey+spug@lopsa.org</a>></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;">
"The advantage" makes it sound like you should be preferring it; you shouldn't. 99%+ of the time, you want C<use>.<br>
</blockquote></div><div><br>Well, I think the "99%+" figure is overstated. 'require' is perfectly appropriate in quite a few situations and is preferred in quite a few more-advanced situations. But, yes, if you are unsure which to use, 'use' is the way to go.<br>
<br>It is rarely the case that replacing 'require' with 'use' will cause you problems (it can cause problems, but it /usually/ doesn't, especially for basic uses) and the reverse is not true (if you have code using 'use', it will often be because 'use' is required and switching to 'require' is likely to break things).<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;">You can use C<require> in a string eval so the load happens at runtime rather than compile time</blockquote>
</div><div><br>No. Simply using 'require' is enough to make the loading happen at run-time. You can also use 'use' 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>
</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<require> to prevent the C<import> method from being called (though ordinarily this isn't something you want to do, you should do a C<use Module qw();> instead to call C<import> with no arguments).</blockquote>
</div><div><br>Actually, qw() like that prevents import() from being called at all. To call import() with no arguments, write simply 'use Module;'. And rather than just stating that 'require' should be avoided, I'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's a very broad question, but I don'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 'require' is not sufficient. So, for a /typical/ OO module, there is very little difference between 'use'ing it vs. 'require'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 'require' is usually unconditionally executed near the very top of the requiring script/module. "perl -c" is one of the rare cases where you would see a difference ("perl -c" will check the syntax of any modules you 'use', but not of any modules you 'require').<br>
<br>If you have compile-time code below that makes use of the module, than you should certainly 'use' the module.<br><br>Oh, let's not forget the "obvious" case: If you import symbols/behaviors from the module, then you need to 'use' the module.<br>
<br>There are certainly plenty of advanced situations where 'require' is a better choice than 'use'. For the basic situations, the main reason I would use 'require' 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 'require' 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. In those situations, it may be worth using 'require' inside of the routine(s) that need the module so that the many runs that don't exercise those features won't need to waste time even loading the unutilized module.<br>
<br>Tye<br></div></div></div>
</div><br></div>