Files
dojo/lib/Dojo/Support.pm

69 lines
1.3 KiB
Perl
Raw Normal View History

2018-07-13 19:06:08 -05:00
package Dojo::Support;
use strict;
use warnings;
use Exporter 'import';
2018-07-19 03:45:46 -05:00
our @EXPORT = qw/ log dmph merge_hash load_module get_names /;
2018-07-13 19:06:08 -05:00
use Mojo::Base 'Mojolicious';
use File::Basename;
use Mojo::File 'path';
use Path::Class;
use Encode qw{ decode_utf8 };
use Hash::Merge;
use Dojo::Conf;
2018-07-19 03:45:46 -05:00
use Mojo::Log;
2018-07-13 19:06:08 -05:00
2018-07-19 03:45:46 -05:00
sub log{
my $log = Mojo::Log->new;
2018-07-20 05:16:47 -05:00
$log->debug("============ ".shift." ===========");
2018-07-19 03:45:46 -05:00
}
2018-07-13 19:06:08 -05:00
sub get_names{
my $dir = shift;
my @file_name;
opendir(DIR, $dir) or die "error opening dir: $dir \n$!";
while (my $file = readdir(DIR)) {
next unless ( $file =~m/^[\w\d]/);
next unless (-f "$dir/$file");
push(@file_name,$file);
}
closedir(DIR);
return sort @file_name;
};
sub merge_hash{
2018-07-19 03:45:46 -05:00
#my ($v,$w)=@_;
my $h={};
2018-07-13 19:06:08 -05:00
my $merger = Hash::Merge->new('LEFT_PRECEDENT');
2018-07-19 03:45:46 -05:00
foreach (@_){
$h= $merger->merge($h,$_);
}
return $h;
2018-07-13 19:06:08 -05:00
}
sub load_module{
2018-07-18 04:28:26 -05:00
my $dir=shift;
2018-07-13 19:06:08 -05:00
my $lpath= Dojo::Conf::MOD."/$dir";
2018-07-18 04:28:26 -05:00
my $h;
2018-07-13 19:06:08 -05:00
$h->{layout} = "default";
my $q;
my @s= get_names($lpath);
foreach (@s) {
push(@{$h->{$1}}, "/$dir/$_") if /.*\.(css|js)$/;
push(@{$q->{$1}}, "/$dir/$_") if /.*\.(q|json|txt|md)$/;
}
return ($h,$q);
};
sub dmph{
my $x = shift;
my $y=" ";
foreach (sort(keys %$x)) {
$y= "$y $_ = $x->{$_} \n";
}
return $y;
}
1;