SHARYA...e::Util(3pm)

πŸ“¦
version 0.56 Β· 2 versions
  • 0.56 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.56 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

🌸 SHARYANTO::File::Util πŸ’…

okay bestie, welcome to SHARYANTO::File::Util β€” it's the File::Util page from the User Contributed Perl Documentation set, and it is genuinely just a lil grab-bag of file-related utilities. no cap. it's giving "short but mighty" energy. πŸ¦‹


✨ VERSION ✨

version 0.56 β€” she's on 0.56, periodt. πŸŽ€


✨ SYNOPSIS ✨

okay so here's the whole vibe in one go:

use SHARYANTO::File::Util qw(file_exists l_abs_path dir_empty);

print "file exists" if file_exists("/path/to/file/or/dir");
print "absolute path = ", l_abs_path("foo");
print "dir exists and is empty" if dir_empty("/path/to/dir");

you pull in the three functions you want, you call them, you print stuff. iconic. that's it, that's the whole flow. 🌈


✨ DESCRIPTION ✨

literally the description section and it's out here saying nothing, which honestly? mood. πŸ™ˆ the real tea is all down in FUNCTIONS, so scroll, bestie.


✨ FUNCTIONS ✨

first things first, the important lore: None are exported by default, but they are exportable. so you have to actually ask for them in your use line, they're not just gonna show up uninvited. main character energy only when summoned. πŸ‘‘

file_exists($path) => BOOL

okay so this one is almost the -e test, except it has one very specific little quirk: it treats a symlink whose target doesn't exist as existing. so if sym is a symlink pointing at something that isn't there:

-e "sym"             # false, Perl performs stat() which follows symlink

but then, plot twist:

-l "sym"             # true, Perl performs lstat()
-e _                 # false

the way that -e follows the symlink and goes "nope, nothing there" while -l is like "girl, the link is right there" 😭 so this function does this test instead:

!(-l "sym") && (-e _) || (-l _)

no notes, that's the whole logic. 🧚

l_abs_path($path) => STR

basically Cwd::abs_path() but with boundaries: it will not follow the symlink if $path is a symlink. it will still follow symlinks for the parent paths though, so don't get it twisted. πŸ’…

example time:

use Cwd qw(getcwd abs_path);

say getcwd();              # /home/steven
# s is a symlink to /tmp/foo
say abs_path("s");         # /tmp/foo
say l_abs_path("s");       # /home/steven/s
# s2 is a symlink to /tmp
say abs_path("s2/foo");    # /tmp/foo
say l_abs_path("s2/foo");  # /tmp/foo

see how abs_path("s") chases the link all the way to /tmp/foo, but l_abs_path("s") is like "no, we stay home, it's /home/steven/s" 😌 meanwhile for s2/foo both of them land on /tmp/foo because the symlink is in a parent path, not the final component.

Mnemonic so you never forget it: l_abs_path -> abs_path is analogous to lstat -> stat. iykyk. πŸ”₯

Note: currently uses hardcoded "/" as path separator. so yeah, that's a thing. noted. ⭐

dir_empty($dir) => BOOL

returns true if $dir exists and is empty. that's the whole assignment. clean, simple, slay. ✨

and just to say it again down here: None are exported by default, but they are exportable. 🫢


✨ FAQ ✨

Where is file_empty()?

great question bestie!! there isn't one, and the answer is: you don't need it. if you wanna check that a path exists, is a regular file, and is empty (content is zero-length), you can just use the -z filetest operator. boom. solved. πŸ’€


✨ AUTHOR ✨

Steven Haryanto <stevenharyanto@gmail.com> β€” the legend behind all of this. πŸ‘‘


This software is copyright (c) 2013 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


and that's the page, babes πŸ’– this was SHARYA...e::Util(3pm) β€” well, SHARYANTO::File::Util(3pm) β€” generated by perl v5.34.0 on 2022-06-17. go check if your dir is empty, you deserve it. πŸŒΈπŸŽ€πŸ¦‹

πŸ“¦ where this page comes from