Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Help Changing use into require/import

by Discipulus (Abbot)
on Mar 07, 2018 at 21:21 UTC ( #1210482=note: print w/replies, xml ) Need Help??


in reply to Help Changing use into require/import

Hello computergeek and welcome to the monastery and to the wonderful world of Perl!

You can also be interested into Module::Load::Conditional

PS also note that the folowing works:

perl -e " my $rc; BEGIN{ + $rc = eval{require Capture::Tiny; Capture::Tiny->import ('capture'); 1; } } if ($rc){ ($SpoolFile->[0],$CMDERR[0],$ReturnCode)=capture{sy +stem qq[ echo Hello, World | cat ]}; print join ', ', $ReturnCode, $SpoolFile->[0]; } " 0, Hello, World

Infact use is equivalent to BEGIN { require Module; Module->import( LIST ); } as per use docs.

PPS see also some other links about compile time / run time

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Help Changing use into require/import
by stevieb (Canon) on Mar 07, 2018 at 21:54 UTC

    I wrote Plugin::Simple, primarily because I had a bit of time and wanted to write my own basic plugin architecture for a learning exercise. Here's a runtime snip of actually loading a plugin. It can be in the form of a file name with a path, a filename without a path, or a standard Module::IAm syntax. The load() function is brought in from Module::Load.

    The conditional load doesn't affect the program, it simply determines whether the plugin name will be returned to the caller:

    sub _load { my ($self, $plugin) = @_; if ($plugin =~ /(.*)\W(\w+)\.pm/){ unshift @INC, $1; $plugin = $2; } elsif ($plugin =~ /^(\w+)\.pm$/){ unshift @INC, '.'; $plugin = $1; } my $loaded = eval { load $plugin; 1; }; if ($loaded) { return $plugin; } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2021-04-17 17:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?