Aion-Fs
view release on metacpan or search on metacpan
lib/Aion/Fs.pm view on Meta::CPAN
NLINK_NO => 3, # ÐолиÑеÑÑво жеÑÑкиÑ
ÑÑÑлок
UID_NO => 4, # ÐденÑиÑикаÑÐ¾Ñ Ð¿Ð¾Ð»ÑзоваÑелÑ-владелÑÑа
GID_NO => 5, # ÐденÑиÑикаÑÐ¾Ñ Ð³ÑÑппÑ-владелÑÑа
RDEV_NO => 6, # ÐÐ¾Ð¼ÐµÑ ÑÑÑÑойÑÑва (еÑли ÑÑо ÑпеÑиалÑнÑй Ñайл)
SIZE_NO => 7, # Ð Ð°Ð·Ð¼ÐµÑ Ñайла в байÑаÑ
ATIME_NO => 8, # ÐÑÐµÐ¼Ñ Ð¿Ð¾Ñледнего доÑÑÑпа
MTIME_NO => 9, # ÐÑÐµÐ¼Ñ Ð¿Ð¾Ñледнего изменениÑ
CTIME_NO => 10, # ÐÑÐµÐ¼Ñ Ð¿Ð¾Ñледнего Ð¸Ð·Ð¼ÐµÐ½ÐµÐ½Ð¸Ñ inode
BLKSIZE_NO => 11, # Ð Ð°Ð·Ð¼ÐµÑ Ð±Ð»Ð¾ÐºÐ° ввода-вÑвода
BLOCKS_NO => 12, # ÐолиÑеÑÑво вÑделеннÑÑ
блоков
};
# ÐеÑнÑÑÑ Ð²ÑÐµÐ¼Ñ Ð¼Ð¾Ð´Ð¸ÑикаÑии Ñайла
sub mtime(;$) {
my ($file) = @_ == 0? $_: @_;
($file) = @$file if ref $file;
(Time::HiRes::stat $file)[MTIME_NO] // die "mtime $file: $!"
}
# ÐнÑоÑмаÑÐ¸Ñ Ð¾ Ñайле в виде Ñ
еÑа
sub sta(;$) {
my ($path) = @_ == 0? $_: @_;
($path) = @$path if ref $path;
my %sta = (path => $path);
@sta{qw/dev ino mode nlink uid gid rdev size atime mtime ctime blksize blocks/} = Time::HiRes::stat $path or die "sta $path: $!";
# @sta{qw/
# user_can_exec user_can_read user_can_write
# group_can_exec group_can_read group_can_write
# other_can_exec other_can_read other_can_write
# /} = (
#
# );
\%sta
}
# ФайловÑе ÑилÑÑÑÑ
sub _filters(@) {
map {
if(ref $_ eq "CODE") {$_}
elsif(ref $_ eq "Regexp") { my $re = $_; sub { $_ =~ $re } }
elsif(/^-([a-z]+)$/) {
eval join "", "sub { ", (join " && ", map "-$_()", split //, $1), " }"
}
else { my $re = wildcard(); sub { $_ =~ $re } }
} @_
}
# ÐайÑи ÑайлÑ
sub find(;@) {
my $files = @_? shift: $_;
$files = [$files] unless ref $files;
my @noenters; my $errorenter = sub {};
my $ex = @_ && ref($_[$#_]) eq 'Aion::Fs::Find'
? pop
: undef;
if($ex) {
bless $ex, 'Aion::Fs';
if(Scalar::Util::reftype $ex eq 'CODE') {
$errorenter = $ex;
} else {
$errorenter = bless pop @$ex, undef if Scalar::Util::reftype($ex->[$#$ex]) eq "CODE";
push @noenters, _filters @$ex;
}
}
my @filters = _filters @_;
my $iter = Aion::Fs::Find->new(
noenters => \@noenters,
errorenter => $errorenter,
filters => \@filters,
files => $files,
);
defined(wantarray)
? (wantarray? @$iter: $iter)
: do { while(defined $iter->next) {} };
}
# Ðе вÑ
одиÑÑ Ð² подкаÑалоги
sub noenter(@) {
bless [@_], "Aion::Fs::Find"
}
# ÐÑзÑваеÑÑÑ Ð´Ð»Ñ Ð²ÑеÑ
оÑибок ввода-вÑвода
sub errorenter(&) {
bless shift, "Aion::Fs::Find"
}
# ÐÑÑÐ°Ð½Ð°Ð²Ð»Ð¸Ð²Ð°ÐµÑ find бÑдÑÑи вÑзван Ñ Ð¾Ð´Ð½Ð¾Ð³Ð¾ из его ÑилÑÑÑов, errorenter или noenter
sub find_stop() {
die bless \(my $stop = 1), "Aion::Fs::Find"
}
# ÐÑÐ¾Ð¸Ð·Ð²Ð¾Ð´Ð¸Ñ Ð·Ð°Ð¼ÐµÐ½Ñ Ð²Ð¾ вÑеÑ
ÑказаннÑÑ
ÑайлаÑ
. ÐозвÑаÑÐ°ÐµÑ ÑÐ°Ð¹Ð»Ñ Ð² коÑоÑÑÑ
замен не бÑло
sub replace(&@) {
my $fn = shift;
my @noreplace; local $_; my $pkg = caller;
my $aref = "$pkg\::a"; my $bref = "$pkg\::b";
for $$aref (@_) {
if(ref $$aref) { ($$aref, $$bref) = @$$aref } else { $$bref = ":utf8" }
my $file = $_ = cat [$$aref, $$bref];
$fn->();
if($file ne $_) { lay [$$aref, $$bref], $_ } else { push @noreplace, $$aref if defined wantarray }
}
@noreplace
}
# СÑиÑÐ°ÐµÑ Ð²Ñе ÑказаннÑе ÑайлÑ. ÐозвÑаÑÐ°ÐµÑ Ð¿ÐµÑеданнÑе ÑайлÑ
sub erase(@) {
-d? rmdir: unlink or die "erase ${\(-d? 'dir': 'file')} $_: $!" for @_;
@_
}
# ÐеÑÐµÐ²Ð¾Ð´Ð¸Ñ Ð²Ð¸Ð»Ð´ÐºÐ°Ñд в ÑегÑлÑÑкÑ
sub wildcard(;$) {
my ($wildcard) = @_;
$wildcard = $_ if @_ == 0;
$wildcard =~ s{
(?<file> \*\*)
| (?<path> \*)
| (?<anyn> \?\? )
| (?<any> \? )
| (?<w1> \{ )
| (?<w2> \} )
| (?<comma> , )
| .
}{
exists $+{file}? "[^/]*?":
exists $+{path}? ".*?":
exists $+{anyn}? "[^/]":
exists $+{any}? ".":
exists $+{w1}? "(":
exists $+{w2}? ")":
exists $+{comma}? "|":
quotemeta $&
}gxe;
qr/^$wildcard$/ns
}
# ÐÑкÑÑÐ²Ð°ÐµÑ Ñайл на Ñказанной ÑÑÑоке в ÑедакÑоÑе
use config EDITOR => "vscodium %p:%l";
sub goto_editor($$) {
my ($path, $line) = @_;
my $p = EDITOR;
$p =~ s!%p!$path!;
$p =~ s!%l!$line!;
my $status = system $p;
die "$path:$line --> $status" if $status;
return;
}
lib/Aion/Fs.pm view on Meta::CPAN
=item * L<Path::Util> - C<$filename = basename($dir)>.
=item * L<Parse::Path> â C<< Parse::Path-E<gt>new(path =E<gt> 'gophers[0].food.count', style =E<gt> 'DZIL')-E<gt>push("chunk") >>. Works with paths as arrays (C<push>, C<pop>, C<shift>, C<splice>). It also overloads comparison operators. It has sty...
=back
=head2 transpath ($path?, $from, $to)
Converts a path from one OS format to another.
If C<$path> is not specified, C<$_> is used.
For a list of supported operating systems, see the examples of the C<path> subroutine just above or like this: C<keys %Aion::Fs::FS>.
OS names are case insensitive.
local $_ = ">x>y>z.doc.zip";
transpath "vos", "unix" # \> /x/y/z.doc.zip
transpath "vos", "VMS" # \> [.x.y]z.doc.zip
transpath $_, "vos", "RiscOS" # \> .x.y.z/doc/zip
=head2 splitdir (;$dir)
Splits a directory into components. The directory should first be obtained from C<< path-E<gt>{dir} >>.
local $^O = "unix";
[ splitdir "/x/" ] # --> ["", "x", ""]
=head2 joindir (;$dirparts)
Combines a directory from its components. The resulting directory should then be included in C<< path +{dir =E<gt> $dir} >>.
local $^O = "unix";
joindir qw/x y z/ # => x/y/z
path +{ dir => joindir qw/x y z/ } # => x/y/z/
=head2 splitext (;$ext)
Breaks the extension into its components. The extension should first be obtained from C<< path-E<gt>{ext} >>.
local $^O = "unix";
[ splitext ".x." ] # --> ["", "x", ""]
=head2 joinext (;$extparts)
Combines an extension from its components. The resulting extension should then be included in C<< path +{ext =E<gt> $ext} >>.
local $^O = "unix";
joinext qw/x y z/ # => x.y.z
path +{ ext => joinext qw/x y z/ } # => .x.y.z
=head2 include (;$pkg)
Connects C<$pkg> (if it has not already been connected via C<use> or C<require>) and returns it. Without a parameter, uses C<$_>.
lib/A.pm file:
package A;
sub new { bless {@_}, shift }
1;
lib/N.pm file:
package N;
sub ex { 123 }
1;
use lib "lib";
include("A")->new # ~> A=HASH\(0x\w+\)
[map include, qw/A N/] # --> [qw/A N/]
{ local $_="N"; include->ex } # -> 123
=head2 catonce (;$file)
Reads the file for the first time. Any subsequent attempt to read this file returns C<undef>. Used to insert js and css modules into the resulting file. Without a parameter, uses C<$_>.
=over
=item * C<$file> can contain arrays of two elements. The first is considered as a path, and the second as a layer. The default layer is C<:utf8>.
=item * If C<$file> is not specified, use C<$_>.
=back
local $_ = "catonce.txt";
lay "result";
catonce # -> "result"
catonce # -> undef
eval { catonce[] }; $@ # ~> catonce not use ref path!
=head2 wildcard (;$wildcard)
Converts a file mask to a regular expression. Without a parameter, uses C<$_>.
=over
=item * C<**> - C<[^/]*>
=item * C<*> - C<.*>
=item * C<?> - C<.>
=item * C<??> - C<[^/]>
=item * C<{> - C<(>
=item * C<}> - C<)>
=item * C<,> - C<|>
=item * Other characters are escaped using C<quotemeta>.
=back
wildcard "*.{pm,pl}" # \> (?^usn:^.*?\.(pm|pl)$)
wildcard "?_??_**" # \> (?^usn:^._[^/]_[^/]*?$)
( run in 1.613 second using v1.01-cache-2.11-cpan-98e64b0badf )