| 1 | package Crop::Util; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Crop::Util - Utility functions for the Crop framework | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Crop::Util; | ||
| 12 | # ...usage... | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | Crop::Util provides general utility functions for the Crop framework. | ||
| 17 | |||
| 18 | =head1 AUTHORS | ||
| 19 | |||
| 20 | Euvgenio (Core Developer) | ||
| 21 | |||
| 22 | Alex (Contributor) | ||
| 23 | |||
| 24 | =head1 COPYRIGHT AND LICENSE | ||
| 25 | |||
| 26 | Apache 2.0 | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | use base qw/ Exporter /; | ||
| 31 | |||
| 32 | =begin nd | ||
| 33 | Package: Crop::Util | ||
| 34 | General purose functions. | ||
| 35 | |||
| 36 | Non-OOP module. | ||
| 37 | =cut | ||
| 38 | |||
| 39 | use strict; | ||
| 40 | use v5.14; | ||
| 41 | |||
| 42 | use Crop::Error; | ||
| 43 | use Crop::Debug; | ||
| 44 | |||
| 45 | =begin nd | ||
| 46 | Variable: our @EXPORT_OK | ||
| 47 | Functions exported by order: | ||
| 48 | |||
| 49 | - expose_hashes | ||
| 50 | - load_class | ||
| 51 | - split_by_3 | ||
| 52 | - trim | ||
| 53 | =cut | ||
| 54 | our @EXPORT = qw/ expose_hashes /; | ||
| 55 | our @EXPORT_OK = qw/ load_class /; | ||
| 56 | |||
| 57 | =begin nd | ||
| 58 | Method: expose_hashes (\@hash) | ||
| 59 | Expose arrayref to hash. | ||
| 60 | |||
| 61 | References in even position will be dereferenced. | ||
| 62 | |||
| 63 | Parameters: | ||
| 64 | \@hash - hash elements packed to an array | ||
| 65 | |||
| 66 | Returns: | ||
| 67 | hash reference | ||
| 68 | =cut | ||
| 69 | sub expose_hashes { | ||
| 70 | my $src = shift; | ||
| 71 | return warn "|ERR: expose_hashes() expects one argument exactly" if @_; | ||
| 72 | return warn "|ERR: expose_hashes() expects arrayref" unless ref $src eq 'ARRAY'; | ||
| 73 | |||
| 74 | my (@dst, $position); | ||
| 75 | for (@$src) { | ||
| 76 | if (++$position % 2 and ref $_ eq 'HASH') { # expose reference in a key position | ||
| 77 | push @dst, %$_; | ||
| 78 | ++$position; | ||
| 79 | } else { | ||
| 80 | push @dst, $_; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | +{@dst}; # ref - not a block instruction | ||
| 85 | } | ||
| 86 | |||
| 87 | =begin nd | ||
| 88 | Function: load_class ($module) | ||
| 89 | Load a class by name. | ||
| 90 | |||
| 91 | >load_class(My::Module) | ||
| 92 | |||
| 93 | Parameters: | ||
| 94 | $module - name of module in form of Perl (My::Module) | ||
| 95 | |||
| 96 | Returns: | ||
| 97 | true - if module successed | ||
| 98 | false - otherwise | ||
| 99 | =cut | ||
| 100 | sub load_class { | ||
| 101 | my $module = shift; | ||
| 102 | |||
| 103 | $module =~ s!::!/!g; | ||
| 104 | $module .= '.pm'; | ||
| 105 | |||
| 106 | require $module or warn "OBJECT: Failed to load a module from $module"; | ||
| 107 | } | ||
| 108 | |||
| 109 | 1; |