/usr/share/perl5/Mojo/Home.pm is in libmojolicious-perl 2.23-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | package Mojo::Home;
use Mojo::Base -base;
use overload
'bool' => sub {1},
'""' => sub { shift->to_string },
fallback => 1;
use Cwd 'abs_path';
use File::Basename 'dirname';
use File::Find 'find';
use File::Spec;
use FindBin;
use Mojo::Asset::File;
use Mojo::Command;
use Mojo::Loader;
has app_class => 'Mojo::HelloWorld';
# "I'm normally not a praying man, but if you're up there,
# please save me Superman."
sub new { shift->SUPER::new()->parse(@_) }
sub detect {
my ($self, $class) = @_;
# Class
$self->app_class($class) if $class;
$class ||= $self->app_class;
# Environment variable
if ($ENV{MOJO_HOME}) {
my @parts = File::Spec->splitdir(abs_path $ENV{MOJO_HOME});
$self->{parts} = \@parts;
return $self;
}
# Try to find home from lib directory
if ($class) {
# Load
my $file = Mojo::Command->class_to_path($class);
unless ($INC{$file}) {
if (my $e = Mojo::Loader->load($class)) { die $e if ref $e }
}
# Detect
if (my $path = $INC{$file}) {
# Directory
$path =~ s/$file$//;
my @home = File::Spec->splitdir($path);
# Remove "lib" and "blib"
while (@home) {
last unless $home[-1] =~ /^b?lib$/ || $home[-1] eq '';
pop @home;
}
# Turn into absolute path
$self->{parts} =
[File::Spec->splitdir(abs_path(File::Spec->catdir(@home) || '.'))];
}
}
# FindBin fallback
$self->{parts} = [split /\//, $FindBin::Bin] unless $self->{parts};
return $self;
}
sub lib_dir {
my $self = shift;
# Directory found
my $parts = $self->{parts} || [];
my $path = File::Spec->catdir(@$parts, 'lib');
return $path if -d $path;
# No lib directory
return;
}
sub list_files {
my ($self, $dir) = @_;
# Files relative to directory
my $parts = $self->{parts} || [];
my $root = File::Spec->catdir(@$parts);
$dir = File::Spec->catdir($root, split '/', ($dir || ''));
return [] unless -d $dir;
my @files;
find {
wanted => sub {
my @parts =
File::Spec->splitdir(File::Spec->abs2rel($File::Find::name, $dir));
push @files, join '/', @parts unless $parts[-1] =~ /^\./;
},
no_chdir => 1
}, $dir;
return [sort @files];
}
sub mojo_lib_dir { File::Spec->catdir(dirname(__FILE__), '..') }
sub parse {
my ($self, $path) = @_;
return $self unless defined $path;
$self->{parts} = [File::Spec->splitdir($path)];
return $self;
}
sub rel_dir {
my $self = shift;
my $parts = $self->{parts} || [];
File::Spec->catdir(@$parts, split '/', shift);
}
sub rel_file {
my $self = shift;
my $parts = $self->{parts} || [];
File::Spec->catfile(@$parts, split '/', shift);
}
sub slurp_rel_file {
Mojo::Asset::File->new(path => shift->rel_file(@_))->slurp;
}
sub to_string {
my $self = shift;
my $parts = $self->{parts} || [];
File::Spec->catdir(@$parts);
}
1;
__END__
=head1 NAME
Mojo::Home - Detect and access the project root directory in Mojo
=head1 SYNOPSIS
use Mojo::Home;
my $home = Mojo::Home->new;
$home->detect;
=head1 DESCRIPTION
L<Mojo::Home> is a container for home directories.
=head1 ATTRIBUTES
L<Mojo::Home> implements the following attributes.
=head2 C<app_class>
my $class = $home->app_class;
$home = $home->app_class('Foo::Bar');
Application class.
=head1 METHODS
L<Mojo::Home> inherits all methods from L<Mojo::Base> and implements the
following new ones.
=head2 C<new>
my $home = Mojo::Home->new;
my $home = Mojo::Home->new('/foo/bar');
Construct a new L<Mojo::Home> object.
=head2 C<detect>
$home = $home->detect;
$home = $home->detect('My::App');
Detect home directory from application class.
=head2 C<lib_dir>
my $path = $home->lib_dir;
Path to C<lib> directory.
=head2 C<list_files>
my $files = $home->list_files;
my $files = $home->list_files('foo/bar');
List all files in directory and subdirectories recursively.
=head2 C<mojo_lib_dir>
my $path = $home->mojo_lib_dir;
Path to C<lib> directory in which L<Mojolicious> is installed.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<parse>
$home = $home->parse('/foo/bar');
Parse path.
=head2 C<rel_dir>
my $path = $home->rel_dir('foo/bar');
Generate absolute path for relative directory.
=head2 C<rel_file>
my $path = $home->rel_file('foo/bar.html');
Generate absolute path for relative file.
=head2 C<slurp_rel_file>
my $string = $home->slurp_rel_file('foo/bar.html');
Read all file data at once.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<to_string>
my $string = $home->to_string;
my $string = "$home";
Home directory.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|