Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Use 'use' in foreach

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


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

A little bit modified, you can/should also check before loading if it is already loaded:

for my $mod (@mods) { if(!defined($mod->VERSION)) { load $mod; print "$mod version is " . $mod->VERSION . "\n"; } }
"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^3: Use 'use' in foreach
by hippo (Bishop) on Jul 19, 2017 at 12:39 UTC
    you can/should also check before loading if it is already loaded

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

    If I simply double up the array of modules the script runs without error or warning and produces the expected results:

    #!/usr/bin/env perl use strict; use warnings; use Module::Load; my @mods = ('JSON', 'CGI::Lite'); for my $mod (@mods, @mods) { load $mod; print "$mod version is " . $mod->VERSION . "\n"; }

      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."

        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.

        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://1195432]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-23 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found