Aion-Fs
view release on metacpan or search on metacpan
lib/Aion/Fs.pm view on Meta::CPAN
=item * The remaining lines are turned by the C<wildcard> function (see below) into a regular expression to test each path.
=back
Paths that fail the C<@filters> check are not returned.
If the -X filter is not a perl file function, an exception is thrown:
eval { find "example", "-h" }; $@ # ~> Undefined subroutine &Aion::Fs::h called
In this example, C<find> cannot enter the subdirectory and passes an error to the C<errorenter> function (see below) with the C<$_> and C<$!> variables set (to the directory path and the OS error message).
B<Attention!> If C<errorenter> is not specified, then all errors are B<ignored>!
mkpath ["example/", 0];
[find "example"] # --> ["example"]
[find "example", noenter "-d"] # --> ["example"]
eval { find "example", errorenter { die "find $_: $!" } }; $@ # ~> find example: Permission denied
mkpath for qw!ex/1/11 ex/1/12 ex/2/21 ex/2/22!;
my $count = 0;
find "ex", sub { find_stop if ++$count == 3; 1};
$count # -> 3
=head3 See also
=over
=item * L<AudioFile::Find> â searches for audio files in the specified directory. Allows you to filter them by attributes: title, artist, genre, album and track.
=item * L<Directory::Iterator> â C<< $it = Directory::Iterator-E<gt>new($dir, %opts); push @paths, $_ while E<lt>$itE<gt> >>.
=item * L<IO::All> â C<< @paths = map { "$_" } grep { -f $_ && $_-E<gt>size E<gt> 10*1024 } io(".")-E<gt>all(0) >>.
=item * L<IO::All::Rule> â C<< $next = IO::All::Rule-E<gt>new-E<gt>file-E<gt>size("E<gt>10k")-E<gt>iter($dir1, $dir2); push @paths, "$f" while $f = $next-E<gt>() >>.
=item * L<File::Find> â C<find( sub { push @paths, $File::Find::name if /\.png/ }, $dir )>.
=item * L<File::Find::utf8> â like L<File::Find>, only file paths are in I<utf8>.
=item * L<File::Find::Age> â sorts files by modification time (inherits L<File::Find::Rule>): C<< File::Find::Age-E<gt>in($dir1, $dir2) >>.
=item * L<File::Find::Declare> â C<< @paths = File::Find::Declare-E<gt>new({ size =E<gt> 'E<gt>10K', perms =E<gt> 'wr-wr-wr-', modified =E<gt> 'E<lt>2010-01-30', recurse =E<gt> 1, dirs =E<gt> [$dir1] })-E<gt>find >>.
=item * L<File::Find::Iterator> â has an OOP interface with an iterator and the C<imap> and C<igrep> functions.
=item * L<File::Find::Match> â calls a handler for each matching filter. Similar to C<switch>.
=item * L<File::Find::Node> â traverses the file hierarchy in parallel by several processes: C<< tie @paths, IPC::Shareable, { key =E<gt> "GLUE STRING", create =E<gt> 1 }; File::Find::Node-E<gt>new(".")-E<gt>process(sub { my $f = shift; $f-E<gt>for...
=item * L<File::Find::Fast> â C<@paths = @{ find($dir) }>.
=item * L<File::Find::Object> â has an OOP interface with an iterator.
=item * L<File::Find::Parallel> â can compare two directories and return their union, intersection and quantitative intersection.
=item * L<File::Find::Random> â selects a file or directory at random from the file hierarchy.
=item * L<File::Find::Rex> â C<< @paths = File::Find::Rex-E<gt>new(recursive =E<gt> 1, ignore_hidden =E<gt> 1)-E<gt>query($dir, qr/^b/i) >>.
=item * L<File::Find::Rule> â C<< @files = File::Find::Rule-E<gt>any( File::Find::Rule-E<gt>file-E<gt>name('*.mp3', '*.ogg')-E<gt>size('E<gt>2M'), File::Find::Rule-E<gt>empty )-E<gt>in($dir1, $dir2); >>. Has an iterator, procedural interface and ex...
=item * L<File::Find::Wanted> â C<@paths = find_wanted( sub { -f && /\.png/ }, $dir )>.
=item * L<File::Hotfolder> â C<< watch( $dir, callback =E<gt> sub { push @paths, shift } )-E<gt>loop >>. Powered by C<AnyEvent>. Customizable. There is parallelization into several processes.
=item * L<File::Mirror> â also forms a parallel path for copying files: C<recursive { my ($src, $dst) = @_; push @paths, $src } '/path/A', '/path/B'>.
=item * L<File::Set> â C<< $fs = File::Set-E<gt>new; $fs-E<gt>add($dir); @paths = map { $_-E<gt>[0] } $fs-E<gt>get_path_list >>.
=item * L<File::Wildcard> â C<< $fw = File::Wildcard-E<gt>new(exclude =E<gt> qr/.svn/, case_insensitive =E<gt> 1, sort =E<gt> 1, path =E<gt> "src///*.cpp", match =E<gt> qr(^src/(.*?)\.cpp$), derive =E<gt> ['src/$1.o','src/$1.hpp']); push @paths, $f...
=item * L<File::Wildcard::Find> â C<findbegin($dir); push @paths, $f while $f = findnext()> or C<findbegin($dir); @paths = findall()>.
=item * L<File::Util> â C<< File::Util-E<gt>new-E<gt>list_dir($dir, qw/ --pattern=\.txt$ --files-only --recurse /) >>.
=item * L<Mojo::File> â C<< say for path($path)-E<gt>list_tree({hidden =E<gt> 1, dir =E<gt> 1})-E<gt>each >>.
=item * L<Path::Find> â C<@paths = path_find( $dir, "*.png" )>. For complex queries, use I<matchable>: C<< my $sub = matchable( sub { my( $entry, $directory, $fullname, $depth ) = @_; $depth E<lt>= 3 } >>.
=item * L<Path::Extended::Dir> â C<< @paths = Path::Extended::Dir-E<gt>new($dir)-E<gt>find('*.txt') >>.
=item * L<Path::Iterator::Rule> â C<< $i = Path::Iterator::Rule-E<gt>new-E<gt>file; @paths = $i-E<gt>clone-E<gt>size("E<gt>10k")-E<gt>all(@dirs); $i-E<gt>size("E<lt>10k")... >>.
=item * L<Path::Class::Each> â C<< dir($dir)-E<gt>each(sub { push @paths, "$_" }) >>.
=item * L<Path::Class::Iterator> â C<< $i = Path::Class::Iterator-E<gt>new(root =E<gt> $dir, depth =E<gt> 2); until ($i-E<gt>done) { push @paths, $i-E<gt>next-E<gt>stringify } >>.
=item * L<Path::Class::Rule> â C<< @paths = Path::Class::Rule-E<gt>new-E<gt>file-E<gt>size("E<gt>10k")-E<gt>all($dir) >>.
=back
=head2 noenter (@filters)
Tells C<find> not to enter directories matching the filters behind it.
=head2 errorenter (&block)
Calls C<&block> for every error that occurs when a directory cannot be entered.
=head2 find_stop ()
Stops C<find> being called in one of its filters, C<errorenter> or C<noenter>.
my $count = 0;
find "ex", sub { find_stop if ++$count == 3; 1};
$count # -> 3
=head2 erase (@paths)
Removes files and empty directories. Returns C<@paths>. If there is an I/O error, it throws an exception.
eval { erase "/" }; $@ # ~> erase dir /: Device or resource busy
eval { erase "/dev/null" }; $@ # ~> erase file /dev/null: Permission denied
=head3 See also
lib/Aion/Fs.pm view on Meta::CPAN
=head2 goto_editor ($path, $line)
Opens a file in the editor from C<AION_FS_EDITOR> at the specified line. Defaults to C<vscodium %p:%l>.
.env file:
AION_FS_EDITOR = echo %p:%l > ed.txt
goto_editor "mypath", 10;
cat "ed.txt" # => mypath:10\n
eval { goto_editor "`", 1 }; $@ # ~> `:1 --> 512
=head2 from_pkg (;$pkg)
Transfers the packet to the FS path. Without a parameter, uses C<$_>.
from_pkg "Aion::Fs" # => Aion/Fs.pm
[map from_pkg, "Aion::Fs", "A::B::C"] # --> ["Aion/Fs.pm", "A/B/C.pm"]
=head2 to_pkg (;$path)
Translates the path from the FS to the package. Without a parameter, uses C<$_>.
to_pkg "Aion/Fs.pm" # => Aion::Fs
[map to_pkg, "Aion/Fs.md", "A/B/C.md"] # --> ["Aion::Fs", "A::B::C"]
=head2 from_inc (;$pkg)
Translates the packet to the FS path in C<@INC>. The package file must exist in one of the C<@INC> paths. Without a parameter, uses C<$_>.
from_inc "Aion::Fs" # -> $INC{'Aion/Fs.pm'}
[map from_inc, "A::B::C", "Aion::Fs"] # --> [$INC{'Aion/Fs.pm'}]
from_inc "A::B::C" # -> undef
=head2 to_inc (;$path)
Translates the path from FS to C<@INC> into a package. Without a parameter, uses C<$_>.
to_inc $INC{'Aion/Fs.pm'} # => Aion::Fs
[map to_inc,"A/B/C.pm", $INC{'Aion/Fs.pm'}] # --> ["Aion::Fs"]
to_inc 'Aion/Fs.pm' # -> undef
=head2 ilay (;$path)
Creates a file descriptor. It knows how to close as soon as the last link to it disappears.
It also has a C<path> method, which returns the path to the file.
my $test_file = "test_ilay_complete.txt";
my $f = ilay $test_file;
print $f "Line 1\n";
print $f "Line 2\n";
my $std = select $f; $| = 1; select $std;
-s $f # -> 14
$f->path # => test_ilay_complete.txt
fileno($f) > 0 # -> 1
undef $f;
cat $test_file # => Line 1\nLine 2\n
local $_ = [$test_file, ':raw'];
my $f = ilay;
my $str = "string";
my $num = 42;
my $end = "END";
*FD = *$f{IO};
format FD =
@<<<<<<<< @||||| @>>>>>
$str, $num, $end
.
write FD;
$str = 'int';
write FD;
undef *FD;
undef $f;
my $table = << 'TABLE';
string 42 END
int 42 END
TABLE
cat $test_file # -> $table
=head3 See also
=over
=item * LLL<https://perldoc.perl.org/IO::Handle>.
=back
=head2 icat (;$file)
Creates a file descriptor with the ability to auto-close as soon as the last link to it disappears.
It also has a C<path> method that returns the path passed to it.
local $_ = "test_icat_complete.txt";
lay "Line 1\nLine 2\nLine 3\nBinary\x00\x01\x02";
my $f = icat;
my $bytes = read $f, my $buf, 6;
$bytes # -> 6
$buf # => Line 1
( run in 0.995 second using v1.01-cache-2.11-cpan-df04353d9ac )