Path::Iter(3pm)

πŸ“¦
version 0.2 Β· 7 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

✨ Path::Iter πŸ’…

okay bestie, so this is the manual page for Path::Iter β€” it lives in the User Contributed Perl Documentation corner of your system, and it's genuinely one of those little modules that eats. Let's get into it 🫢


🌸 NAME 🌸

Path::Iter β€” Simple Efficient Path Iteration. that's it, that's the whole vibe: it walks paths, and it does it efficiently. iconic.


πŸŽ€ VERSION πŸŽ€

This document describes Path::Iter version 0.2. no cap, that's the number.


✨ SYNOPSIS ✨

here's the whole thing in action, look how clean:

use Path::Iter;

my $fetch = Path::Iter::get_iterator('foo');

while ( my $next_path = $fetch->() ) {
    # do something with $next_path
}

you grab an iterator, you keep calling it, you get paths one at a time. periodt. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

It iterates through the contents of a given path β€” without having to build the entire list first. that's the whole point. no giant array hogging your memory, just vibes and one path at a time. main character energy fr fr.


πŸ‘‘ INTERFACE πŸ‘‘

🌈 Path::Iter::get_iterator()

This hands you back a code reference that, when you call it, returns the next path. it's giving little path vending machine, and honestly? obsessed.

When there are no more paths β€” or when a directory can't be opened and you've told it to stop as soon as that happens β€” it returns false. so your while loop just... ends. graceful. πŸ’–

It takes one or more paths to process, plus an optional argument hashref as the last argument (peep %ARGS below πŸ‘‡).

The paths it returns contain the initial argument and its contents (if any). so no surprises, you get the root you asked for and everything under it.

Assuming 'foo' in the SYNOPSIS, here's what you'd actually iterate through:

is a symlink:   you'd iterate through foo
is a file:      you'd iterate through foo
is a directory: you'd iterate through foo, foo/bar, foo/wop, foo/bar/baz,

(that trailing comma is in the original, bestie β€” I'm not touching it, it's part of the lore πŸ’€)

πŸ’œ %ARGS πŸ’œ

These are all optional. every single one. you can pass none, you can pass some, you can pass all. no judgement here.

πŸ“ errors

This is an array of hashrefs where any errors get put. so nothing fails silently on you β€” it just quietly files the receipts πŸ“

my @err;
my $iter = Path::Iter::get_iterator($path, {errors => \@err});
while(my $next = $iter->()) {
    ...
}
if (@err) {
    ... handle error ...
}

The keys for each error in the array are as follows:

'path'     => $path,
'function' => 'opendir',
'args'     => [\*DIR, $path],
'errno'    => int($!),
'error'    => int($!) . ": $!",

🧚 stop_when_opendir_fails

Boolean. when it's true, it will short circuit the iteration if an opendir() call fails. so like, if a directory won't open, we're done here, no further questions. πŸ›‘

πŸ¦„ readdir_handler

A coderef to be used to process and return directory contents (IE readdir() results). this is your moment to customise what actually shows up in the iteration:

sub {
    my($working_path, @contents) = @_;

    if ($working_path eq '.data') {
        @contents = grep !/^\.private\-.*/, @contents; # ignore .private-... files in .data/ dir
    }

    return sort { $a cmp $b } @contents; # return what is left in sorted order
}

The first argument is the directory and the rest is its contents.

The @contents have already had the $working_path prepended. so don't go prepending it again, bestie, that's how you get foo/foo/foo and we do not want that energy 😭

It should return the contents how you wish them to appear in the iteration. your call, your rules.

By default symlinks are not followed. like, we see them, we respect them, we do not chase them.

This is a coderef that can control how symlinks are handled.

⚠️ !! IF YOU ALTER THE DEFAULT BEHAVIOR YOU WILL NEED TO CHECK FOR LINK LOOPS AND INFINITE RECURSION !! ⚠️

It receives two arguments: the path and a boolean of if it was an initial argument or not.

If it returns true the path will be followed. If the true value is '2' it will be tranformed into its target. (yes, "tranformed" β€” that's the original spelling and I'm keeping it, it's part of the canon πŸŽ€)

⚠️ !! IF YOU ALTER THE DEFAULT BEHAVIOR YOU WILL NEED TO CHECK FOR LINK LOOPS AND INFINITE RECURSION !! ⚠️

Assume we have a link named 'link' whose target is 'path':

sub {
    my ($path, $is_initial_arg) = @_;
    ...
    return; # default behavior = link
}

sub {
    my ($path, $is_initial_arg) = @_;
    ...
    return 1; # link link/dir link/file link/dir/etc
}

sub {
    my ($path, $is_initial_arg) = @_;
    ...
    return 2; # path path/dir path/file path/dir/etc
}
⚠️ !! IF YOU ALTER THE DEFAULT BEHAVIOR YOU WILL NEED TO CHECK FOR LINK LOOPS AND INFINITE RECURSION !! ⚠️

(three times, bestie. they really want you to know. that's not a vibe check, that's a vibe warning 🚨)

πŸ’œ initial

This is a hashref used internally as a lookup of initial args as they are after any initialization. internal business. not your problem, but now you know it exists πŸ’…


🩺 DIAGNOSTICS 🩺

Throws no warnings or errors of its own. so it's not gonna yell at you unprompted. but you can catch internal opendir failures β€” see "errors" and "stop_when_opendir_fails". that's your safety net, use it 🫢


βš™οΈ CONFIGURATION AND ENVIRONMENT βš™οΈ

Path::Iter requires no configuration files or environment variables. nothing to set up, nothing to export, nothing to source in your shell rc. it just works. slay ✨


πŸ“¦ DEPENDENCIES πŸ“¦

File::Spec

that's the whole list. one (1) dependency. lowkey iconic 🌈


🚫 INCOMPATIBILITIES 🚫

None reported. πŸŽ‰


πŸ› BUGS AND LIMITATIONS πŸ›

No bugs have been reported. flawless behaviour, we love to see it πŸ‘‘

Please report any bugs or feature requests to bug-path-iter@rt.cpan.org, or through the web interface at http://rt.cpan.org.


πŸ’– AUTHOR πŸ’–

Daniel Muey "<http://drmuey.com/cpan_contact.pl>"


Copyright (c) 2008, Daniel Muey "<http://drmuey.com/cpan_contact.pl>". All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.


🚨 DISCLAIMER OF WARRANTY 🚨

okay bestie, here comes the legal wall. it's loud, it's all caps, and it's non-negotiable. read it, don't skip it, it's the fine print that actually matters πŸ‘‡

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.


that's the whole page, bestie πŸ’– this one's from perl v5.38.2, dated 2024-03-07, and it's filed under Path::Iter(3pm). go iterate something. slay πŸ¦‹βœ¨

πŸ“¦ where this page comes from