Perl Diver 2.31
Main Environment Variables Perl Default Values Perl Config - Summary Perl Config - Full Installed Modules List Directory uptime Docs

Module Documentation
Details and documentation about a specific module, including version and documentation (if available). Note that while links to perldoc.com and search.cpan.org are provided, the module may be part of a larger distribution. If you reach a File Not Found page on either site, please try the parent module.

Mojo::Loader

Name Mojo::Loader
Version
Located at /usr/local/share/perl5
File /usr/local/share/perl5/Mojo/Loader.pm
Is Core No
Search CPAN for this module Mojo::Loader
Documentation Mojo::Loader
Module Details Mojo::Loader


NAME

Mojo::Loader - Load all kinds of things


SYNOPSIS

  use Mojo::Loader qw(data_section find_modules load_class);
  # Find modules in a namespace
  for my $module (find_modules 'Some::Namespace') {
    # Load them safely
    my $e = load_class $module;
    warn qq{Loading "$module" failed: $e} and next if ref $e;
    # And extract files from the DATA section
    say data_section($module, 'some_file.txt');
  }


DESCRIPTION

the Mojo::Loader manpage is a class loader and plugin framework. Aside from finding modules and loading classes, it allows multiple files to be stored in the DATA section of a class, which can then be accessed individually.

  package Foo;
  1;
  __DATA__
  @@ test.txt
  This is the first file.
  @@ test2.html (base64)
  VGhpcyBpcyB0aGUgc2Vjb25kIGZpbGUu
  @@ test
  This is the
  third file.

Each file has a header starting with @@, followed by the file name and optional instructions for decoding its content. Currently only the Base64 encoding is supported, which can be quite convenient for the storage of binary data.


FUNCTIONS

the Mojo::Loader manpage implements the following functions, which can be imported individually.

data_section

  my $all   = data_section 'Foo::Bar';
  my $index = data_section 'Foo::Bar', 'index.html';

Extract embedded file from the DATA section of a class, all files will be cached once they have been accessed for the first time.

  # List embedded files
  say for keys %{data_section 'Foo::Bar'};

file_is_binary

  my $bool = file_is_binary 'Foo::Bar', 'test.png';

Check if embedded file from the DATA section of a class was Base64 encoded.

find_packages

  my @pkgs = find_packages 'MyApp::Namespace';

Search for packages in a namespace non-recursively.

find_modules

  my @modules = find_modules 'MyApp::Namespace';
  my @modules = find_modules 'MyApp::Namespace', {recursive => 1};

Search for modules in a namespace.

These options are currently available:

recursive
  recursive => 1

Search namespace recursively.

load_class

  my $e = load_class 'Foo::Bar';

Load a class and catch exceptions, returns a false value if loading was successful, a true value if the class was not found, or a the Mojo::Exception manpage object if loading failed. Note that classes are checked for a new method to see if they are already loaded, so trying to load the same class multiple times may yield different results.

  # Handle exceptions
  if (my $e = load_class 'Foo::Bar') {
    die ref $e ? "Exception: $e" : 'Not found!';
  }

load_classes

  my @classes = load_classes 'Foo::Bar';

Load all classes in a namespace recursively.


SEE ALSO

Mojolicious, the Mojolicious::Guides manpage, https://mojolicious.org.

Perl Diver brought to you by ScriptSolutions.com © 1997- 2025