Mojo::Loader - Loader
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'); }
Mojo::Loader 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.
DATA
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.
@@
Mojo::Loader implements the following functions, which can be imported individually.
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.
say for keys %{data_section 'Foo::Bar'};
my $bool = file_is_binary 'Foo::Bar', 'test.png';
Check if embedded file from the DATA section of a class was Base64 encoded.
my @modules = find_modules 'MyApp::Namespace';
Search for modules in a namespace non-recursively.
my $e = load_class 'Foo::Bar';
Load a class and catch exceptions. Note that classes are checked for a new method to see if they are already loaded.
new
if (my $e = load_class 'Foo::Bar') { die ref $e ? "Exception: $e" : 'Not found!'; }
Mojolicious, Mojolicious::Guides, http://mojolicio.us.
To install Mojolicious, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious
CPAN shell
perl -MCPAN -e shell install Mojolicious
For more information on module installation, please visit the detailed CPAN module installation guide.