Module::Want(3pm)

πŸ“¦
version 0.6 Β· 2 versions
  • 0.6 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.6 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ¦‹ NAME πŸ¦‹

Module::Want β€” the little module that checks @INC once for the modules you want but might not actually have πŸ’– (yes bestie, that's the whole pitch, and it ate)


🌸 VERSION 🌸

This doc is describing Module::Want version 0.6 ✨


πŸŽ€ SYNOPSIS πŸŽ€

use Module::Want;

if (have_mod('Encode')) {
    ... use Encode::whatever ...
}
else {
    ... use Encode:: alternative ...
}

πŸ’… DESCRIPTION πŸ’…

Okay so picture this 😭 Sometimes you wanna lazy load a module inside, like, a loop or a function. So first you do the whole eval-require thing… but then you clock that if the module isn't there, it's gonna re-search @INC every single time. And then you're like "fine, I'll add a lexical boolean to my eval" and then you're copy-pasting that same simple logic all over the place. On and on it goes :) it's giving groundhog day fr fr πŸ™ˆ

So this module encapsulates all that logic, meaning have_mod() is basically like eval { require X; 1 } β€” except if the module can't be loaded, it remembers that fact and does not go looking in @INC again on subsequent calls. Obsessed. πŸ‘‘

For example, this right here searches @INC for X.pm every iteration of the loop (ick):

while( ... ) {
    if (eval { require X; 1 }) {
        ... use X code ...
    }
    else {
        ... do X-alternative code ...
    }
}

And this one searches @INC for X.pm once (slay):

while( ... ) {
    if (have_mod('X')) {
        ... use X code ...
    }
    else {
        ... do X-alternative code ...
    }
}

Also β€” and this part is lowkey underrated β€” it will not trip the sig die handler 🫢 which prevents a totally benign eval from tripping a potentially fatal sig handler. (i.e. that's another whole step you'd have to do manually with the straight eval form. No thank you.)


✨ INTERFACE ✨

import() puts have_mod() into the caller's name space. Cute, right?

🌈 have_mod()

Takes the name space to require() if we have not tried already.

Returns true if it could be loaded. False otherwise.

You can give it a second true argument to skip using the value from the last time it was called and re-try require()ing it πŸ”

if (!have_mod('X')) {
    # do some things to try and ger X available
}

if (have_mod('X',1)) {
    # sweet we have it now!
}

πŸ“ import()

You can use() it with a list to call have_mod() on:

use Module::Want qw(X Y Z); # calls have_mod('X'), have_mod('Y'), and have_mod('Z')

🧚 Utility functions

These aren't the real reasons for this module but they've proven useful when you're doing things that would require have_mod() so here they are πŸ’œ

They can all be exported thusly:

use Module::Want qw(is_ns);

For an entire suite if name space utilities see Module::Util and friends.

🌟 is_ns($ns)

Boolean of if '$ns' is a proper name space or not.

if(is_ns($ns)) {
    ... use $ns as a module/class name ...
}
else {
   ... "invalid input please try again" prompt ...
}

🌟 get_ns_regexp()

Returns a quoted Regexp that matches a name space for use in your regexes.

🌟 get_all_use_require_in_text($text)

This will return a list of all name spaces being use()d or require()d in perlish looking $text.

It will also return ones being eval()d in.

It is very simplistic (e.g. it may or may not return use/require statements that are in comments, it will match verbiage in a here doc that start w/ XuseX) so if it does not fit your needs you'll need to try a PPI based solution (I have Perl::DependList on my radar that does just that.). Noted bestie πŸ“

🌟 get_inc_key($ns)

Returns what $ns's key in %INC would be (if is_ns($ns) of course)

if (my $inc_key =  get_inc_key($ns)) {
    if (exists $INC{$inc_key}) {
       ... in %INC ...
    }
    else {
        ... not in %INC ...
    }
}

%INC keys are always unix format so don't panic 🫢

If I've been misinformed of that fact then please let me know, thanks πŸ™

🌟 get_relative_path_of_ns($ns)

Alias of "get_inc_key($ns)" whose name indicates a different intent.

🌟 get_clean_ns($ns)

Takes $ns, trims leading and trailing whitespace and turns ' into ::, and returns the cleaned copy. Sparkly clean ✨

🌟 normalize_ns($ns)

Alias of "get_clean_ns($ns)" whose name indicates a different intent.

🌟 ns2distname($ns)

Turns the given name space into a distribution name.

e.g. Foo::Bar::Baz becomes Foo-Bar-Baz

🌟 distname2ns($distname)

Turns the given distribution name into a name space.

e.g. Foo-Bar-Baz becomes Foo::Bar::Baz

🌟 get_inc_path_via_have_mod($ns)

Return the %INC entryXs value if we have_mod($ns);

🌟 search_inc_paths($ns)

Without loading the module, search for $ns in @INC.

In scalar context returns the first path it is found in, in list context returns all paths it is found in.

my $first_path_it_is_in = search_inc_paths($ns);
my @all_paths_it_is_in  = search_inc_paths($ns);

By default it returns the path it is in without the module-name part of the path. A second true argument will return the entire path.

my $abs_path_of_pm_file = search_inc_paths($ns,1);

πŸ‘‘ DIAGNOSTICS πŸ‘‘

  • "Invalid Namespace" β€” The argument to have_mod() is not a name space

🌈 CONFIGURATION AND ENVIRONMENT 🌈

Module::Want requires no configuration files or environment variables. Nothing. Zero. Peace and quiet 😌


πŸ’– DEPENDENCIES πŸ’–

None.


πŸ¦‹ INCOMPATIBILITIES πŸ¦‹

None reported.


πŸ’€ BUGS AND LIMITATIONS πŸ’€

No bugs have been reported.

Please report any bugs or feature requests to "bug-module-want@rt.cpan.org", or through the web interface at <http://rt.cpan.org>.


πŸ’œ AUTHOR πŸ’œ

Daniel Muey "<http://drmuey.com/cpan_contact.pl>"


Copyright (c) 2010, Daniel Muey "<http://drmuey.com/cpan_contact.pl>". All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.


🚨 DISCLAIMER OF WARRANTY 🚨

Okay bestie, this part is in ALL CAPS for a reason, so read it, don't skim it πŸ™ˆ

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.


okay that's the whole page, mwah πŸ’‹ this was rendered for perl v5.36.0, dated 2022-11-19, and it's the manpage for Module::Want(3pm) πŸŽ€ go forth and have_mod() responsibly, bestie 🫢✨

πŸ“¦ where this page comes from