| 1 | package FindApp::Utils::List; | ||
| 2 | |||
| 3 | use v5.10; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use FindApp::Utils::Carp; | ||
| 8 | use FindApp::Utils::Assert qw( :all ); | ||
| 9 | use FindApp::Utils::Syntax qw( function ); | ||
| 10 | |||
| 11 | ################################################################# | ||
| 12 | |||
| 13 | sub alldir_map ( & ) ; | ||
| 14 | sub colonize ( @ ) ; | ||
| 15 | sub commify_and ; | ||
| 16 | sub commify_nor ; | ||
| 17 | sub commify_or ; | ||
| 18 | sub firsts_not_in_second ( \@ \@ ) ; | ||
| 19 | sub somedir_map ( &@ ) ; | ||
| 20 | sub subdir_map ( & ) ; | ||
| 21 | sub uniq ; | ||
| 22 | sub uniq_files ; | ||
| 23 | |||
| 24 | ################################################################# | ||
| 25 | |||
| 26 | use Exporter qw(import); | ||
| 27 | our $VERSION = v1.0; | ||
| 28 | our @EXPORT_OK = ( | ||
| 29 | <commify_{and,or,nor}>, | ||
| 30 | qw( | ||
| 31 | alldir_map | ||
| 32 | BLM | ||
| 33 | colonize | ||
| 34 | firsts_not_in_second | ||
| 35 | somedir_map | ||
| 36 | subdir_map | ||
| 37 | uniq | ||
| 38 | uniq_files | ||
| 39 | ), | ||
| 40 | ); | ||
| 41 | our %EXPORT_TAGS = ( | ||
| 42 | all => \@EXPORT_OK, | ||
| 43 | ); | ||
| 44 | |||
| 45 | ################################################################# | ||
| 46 | |||
| 47 | sub BLM() { return qw(bin lib man); } | ||
| 48 | |||
| 49 | sub colonize(@) { join(":", @_) } | ||
| 50 | |||
| 51 | sub uniq { | ||
| 52 | my %seen; | ||
| 53 | return grep { !$seen{$_}++ } grep {defined} @_; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub uniq_files { | ||
| 57 | my @input = @_; | ||
| 58 | my %seen; | ||
| 59 | my @output; | ||
| 60 | for my $file (@input) { | ||
| 61 | next if $seen{$file}++; | ||
| 62 | next unless stat $file; | ||
| 63 | my($dev,$ino) = stat _; | ||
| 64 | next if $seen{$dev,$ino}++; | ||
| 65 | push @output, $file; | ||
| 66 | } | ||
| 67 | return @output; | ||
| 68 | } | ||
| 69 | |||
| 70 | # Yes, this isn't working right for undef. That shouldn't be a path element | ||
| 71 | # anyway but there are bugs in Env.pm this is working around. | ||
| 72 | sub firsts_not_in_second(\@ \@) { | ||
| 73 | good_args(@_ == 2); | ||
| 74 | my($aref, $bref) = @_; | ||
| 75 | |||
| 76 | # worst case | ||
| 77 | local $SIG{__WARN__} = sub {}; | ||
| 78 | |||
| 79 | my %seen = map { $_ => 1 } grep {defined} @$bref; | ||
| 80 | return grep { ! $seen{$_} } grep {defined} @$aref; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub somedir_map(&@) { | ||
| 84 | good_args(@_ > 0); | ||
| 85 | my $code = shift; | ||
| 86 | return map { &$code } @_; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub subdir_map(&) { | ||
| 90 | good_args(@_ == 1); | ||
| 91 | my $code = shift; | ||
| 92 | return somedir_map(\&$code, BLM); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub alldir_map(&) { | ||
| 96 | good_args(@_ == 1); | ||
| 97 | my $code = shift; | ||
| 98 | return somedir_map(\&$code, root => BLM); | ||
| 99 | } | ||
| 100 | |||
| 101 | ################################################################# | ||
| 102 | |||
| 103 | for my $CONJ (qw(and or nor)) { | ||
| 104 | function "commify_$CONJ" => sub { | ||
| 105 | my $comma = "@_" =~ /,/ ? ";" : ","; | ||
| 106 | (@_ == 0) ? "" : | ||
| 107 | (@_ == 1) ? $_[0] : | ||
| 108 | (@_ == 2) ? join(" $CONJ " => @_) : | ||
| 109 | join("$comma " => @_[0 .. ($#_-1)], | ||
| 110 | "$CONJ $_[-1]" ); | ||
| 111 | }; | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; | ||
| 115 | |||
| 116 | =encoding utf8 | ||
| 117 | |||
| 118 | =head1 NAME | ||
| 119 | |||
| 120 | FindApp::Utils::List - FIXME | ||
| 121 | |||
| 122 | =head1 SYNOPSIS | ||
| 123 | |||
| 124 | use FindApp::Utils::List; | ||
| 125 | |||
| 126 | =head1 DESCRIPTION | ||
| 127 | |||
| 128 | =head2 Exports | ||
| 129 | |||
| 130 | =over | ||
| 131 | |||
| 132 | =item alldir_map | ||
| 133 | |||
| 134 | =item BLM | ||
| 135 | |||
| 136 | =item colonize | ||
| 137 | |||
| 138 | =item commify_and | ||
| 139 | |||
| 140 | =item commify_nor | ||
| 141 | |||
| 142 | =item commify_or | ||
| 143 | |||
| 144 | =item firsts_not_in_second | ||
| 145 | |||
| 146 | =item somedir_map | ||
| 147 | |||
| 148 | =item subdir_map | ||
| 149 | |||
| 150 | =item uniq | ||
| 151 | |||
| 152 | =item uniq_files | ||
| 153 | |||
| 154 | =back | ||
| 155 | |||
| 156 | =head1 EXAMPLES | ||
| 157 | |||
| 158 | =head1 ENVIRONMENT | ||
| 159 | |||
| 160 | =head1 SEE ALSO | ||
| 161 | |||
| 162 | =over | ||
| 163 | |||
| 164 | =item L<FindApp> | ||
| 165 | |||
| 166 | =back | ||
| 167 | |||
| 168 | =head1 CAVEATS AND PROVISOS | ||
| 169 | |||
| 170 | =head1 BUGS AND LIMITATIONS | ||
| 171 | |||
| 172 | =head1 HISTORY | ||
| 173 | |||
| 174 | =head1 AUTHOR | ||
| 175 | |||
| 176 | Tom Christiansen << <tchrist@perl.com> >> | ||
| 177 | |||
| 178 | =head1 LICENCE AND COPYRIGHT | ||
| 179 | |||
| 180 | Copyright (c) 2016, Tom Christiansen C<< <tchrist@perl.com> >>. | ||
| 181 | All Rights Reserved. | ||
| 182 | |||
| 183 | This module is free software; you can redistribute it and/or | ||
| 184 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||
| 185 | |||
| 186 | =cut | ||
| 187 |