.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Mojo::Loader 3pm" .TH Mojo::Loader 3pm 2024-05-15 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Mojo::Loader \- Load all kinds of things .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Mojo::Loader qw(data_section find_modules load_class); \& \& # Find modules in a namespace \& for my $module (find_modules \*(AqSome::Namespace\*(Aq) { \& \& # 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, \*(Aqsome_file.txt\*(Aq); \& } .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" 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 \f(CW\*(C`DATA\*(C'\fR section of a class, which can then be accessed individually. .PP .Vb 1 \& package Foo; \& \& 1; \& _\|_DATA_\|_ \& \& @@ test.txt \& This is the first file. \& \& @@ test2.html (base64) \& VGhpcyBpcyB0aGUgc2Vjb25kIGZpbGUu \& \& @@ test \& This is the \& third file. .Ve .PP Each file has a header starting with \f(CW\*(C`@@\*(C'\fR, 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. .SH FUNCTIONS .IX Header "FUNCTIONS" Mojo::Loader implements the following functions, which can be imported individually. .SS data_section .IX Subsection "data_section" .Vb 2 \& my $all = data_section \*(AqFoo::Bar\*(Aq; \& my $index = data_section \*(AqFoo::Bar\*(Aq, \*(Aqindex.html\*(Aq; .Ve .PP Extract embedded file from the \f(CW\*(C`DATA\*(C'\fR section of a class, all files will be cached once they have been accessed for the first time. .PP .Vb 2 \& # List embedded files \& say for keys %{data_section \*(AqFoo::Bar\*(Aq}; .Ve .SS file_is_binary .IX Subsection "file_is_binary" .Vb 1 \& my $bool = file_is_binary \*(AqFoo::Bar\*(Aq, \*(Aqtest.png\*(Aq; .Ve .PP Check if embedded file from the \f(CW\*(C`DATA\*(C'\fR section of a class was Base64 encoded. .SS find_packages .IX Subsection "find_packages" .Vb 1 \& my @pkgs = find_packages \*(AqMyApp::Namespace\*(Aq; .Ve .PP Search for packages in a namespace non-recursively. .SS find_modules .IX Subsection "find_modules" .Vb 2 \& my @modules = find_modules \*(AqMyApp::Namespace\*(Aq; \& my @modules = find_modules \*(AqMyApp::Namespace\*(Aq, {recursive => 1}; .Ve .PP Search for modules in a namespace. .PP These options are currently available: .IP recursive 2 .IX Item "recursive" .Vb 1 \& recursive => 1 .Ve .Sp Search namespace recursively. .SS load_class .IX Subsection "load_class" .Vb 1 \& my $e = load_class \*(AqFoo::Bar\*(Aq; .Ve .PP 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 Mojo::Exception object if loading failed. Note that classes are checked for a \f(CW\*(C`new\*(C'\fR method to see if they are already loaded, so trying to load the same class multiple times may yield different results. .PP .Vb 4 \& # Handle exceptions \& if (my $e = load_class \*(AqFoo::Bar\*(Aq) { \& die ref $e ? "Exception: $e" : \*(AqNot found!\*(Aq; \& } .Ve .SS load_classes .IX Subsection "load_classes" .Vb 1 \& my @classes = load_classes \*(AqFoo::Bar\*(Aq; .Ve .PP Load all classes in a namespace recursively. .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .