On Wed, Mar 04, 2015 at 06:26:41PM -0800, SSC_perl wrote: > So there's only really a difference for loading modules, not > for setting the minimum version number?
There could be a difference if code with side effects is done first. By being done at compile-time, Uri means that use is effectively requiring the module with a BEGIN block. That means that it will execute before any other code that isn't in a BEGIN block. That could matter in rare, silly cases. In most cases, it wouldn't really matter (usually we "require" modules and assert versions at the beginning of a program or module before anything else is actually done). A silly, contrived program might be: use strict; use warnings; use My::Stuff; my $stuff = My::Stuff->new(); $stuff->do_consequences(); require v5.018; $stuff->more_consequences(); __END__ If your entire program requires 5.018 to work properly, but perhaps My::Stuff::do_consequences will do something *bad* if run under a previous version of perl then this program would be *dangerous*. The require() is going to happen at run-time *after* the do_consequences method is called. If I had used `use' instead then it would have happened at compile-time, within a BEGIN block, and do_consequences never would have been called. Here is that program again using `use' for the version assertion: use strict; use warnings; use My::Stuff; my $stuff = My::Stuff->new(); $stuff->do_consequences(); use v5.018; ### *** $stuff->more_consequences(); __END__ This is sort of like: BEGIN { require strict; } BEGIN { require warnings; } BEGIN { require My::Stuff; My::Stuff->import(); } my $stuff = My::Stuff->new(); $stuff->do_consequences(); BEGIN { require v5.018; } $stuff->more_consequences(); __END__ BEGIN blocks are always executed before surrounding code so you can think of this program as: require strict; require warnings; require My::Stuff; My::Stuff->import(); require v5.018; ### *** my $stuff = My::Stuff->new(); $stuff->do_consequences(); $stuff->more_consequences(); __END__ This is an over-simplification. Hell, I probably don't fully understand the mechanism either, but the idea is that the BEGIN stuff happens first. Even though within my code I had the do_consequences method call before the v5.018 assertion the actual result is that the Perl version is checked first, and if dissatisfied the program will die() right then and there before any consequences can be carried out. I think that generally you should be using `use' unless you have a specific need to use require directly. `use' will call require() under the surface when needed so to you it's basically the same, but it has added benefits that make sense generally. If you want the action to happen at run-time then require() may be more appropriate, but those cases should be rare. Similarly, require is sort of like a wrapper over `do FILENAME' so there may also be cases where you'd want to use `do' instead, but most of the time use is more appropriate, and less often require is more appropriate. For the detailed explanation take the time to read through the following: perldoc perlmod (search for BEGIN) perldoc -f use perldoc -f require perldoc -f do Then use `use' until you know you need something else. :) Regards, -- Brandon McCaig <bamcc...@gmail.com> <bamcc...@castopulence.org> Castopulence Software <https://www.castopulence.org/> Blog <http://www.bambams.ca/> perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }. q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.}; tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'
signature.asc
Description: Digital signature