Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^4: Use 'use' in foreach

by cavac (Parson)
on Jul 19, 2017 at 13:05 UTC ( [id://1195455]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Use 'use' in foreach
in thread Use 'use' in foreach

You can, but should you? What benefit is gleaned by doing so?

Mostly performance, depending on what you do. For example, my Maplat Webserver stuff loads/configures lots of "web modules" from XML config files on startup. Each "web module" maps to one of a hundred or so perl modules (but many "web modules" use the same perl module, just with different config options on different URIs). It checks if $VERSION is defined, and if not, it loads the perl module, so it doesn't have to load them multiple times. Seems to save a bit of time on startup, which is quite handy while debugging.

My system also notifies me which perl modules are dynamically loaded (instead of already loaded with "use"), which is also quite useful in some circumstances.

"For me, programming in Perl is like my cooking. The result may not always taste nice, but it's quick, painless and it get's food on the table."

Replies are listed 'Best First'.
Re^5: Use 'use' in foreach
by stevieb (Canon) on Jul 19, 2017 at 13:23 UTC

    use will never load a module more than once. It has built-in checking for that, and I'd be willing to bet (without any evidence at all) that allowing it to perform the check would likely be faster than doing your own.

    Also, not all modules will have version information, particularly those that are in-house.

      I don't know if this actually performs a valid comparison, but:

      use warnings; use strict; use Benchmark qw(timethese); use Module::Load; BEGIN { timethese( 1000000000, { my_use => sub { use Data::Dumper; }, my_check => sub { load Data::Dumper if ! $Data::Dumper::VERSION; } } ); }

      Output:

      perl bench_use.pl Benchmark: timing 1000000000 iterations of my_check, my_use... my_check: 24 wallclock secs (27.21 usr + -0.09 sys = 27.12 CPU) @ 36 +873156.34/s (n=1000000000) my_use: -3 wallclock secs ( 0.92 usr + 0.33 sys = 1.25 CPU) @ 800 +000000.00/s (n=1000000000)
Re^5: Use 'use' in foreach
by RonW (Parson) on Jul 19, 2017 at 21:56 UTC

    The Module::Load caveats section implies it uses require internally, so, in theory, is already checking.

    Of course, that doesn't avoid overhead from needlessly running load, so performance-wise, would make sense if Module::Load did it's own check before doing any work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1195455]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found