NetSDS...::File(3pm)
π NetSDS::Util::File π
okay bestie, welcome to NetSDS...::File(3pm) β yes that's the page, she's a Perl module from the User Contributed Perl Documentation era and she is serving. Sit down, grab a strawberry π, and let's get into it.
β¨ NAME β¨
NetSDS::Util::File β file related utilities ποΈβ¨
lowkey the whole personality is "I do things to files and directories so you don't have to."
πΈ SYNOPSIS πΈ
here's the vibe in three lines, no notes:
use NetSDS::Util::File qw(file_read);
my $passwd = file_read('/etc/passwd');
file_move('/etc/passwd', '/tmp/find_this');
see? import what you need, read a file into a scalar, move a file. iconic behaviour. π
π¦ DESCRIPTION π¦
NetSDS::Util::File is the friend who handles all your files-and-directories admin: creating, reading, writing, copying and moving files and catalogs. she's got the whole to-do list covered, fr fr. ππ
and obviously β of course β she's standing on the shoulders of legends. this module uses well known things like File::Spec, File::Path, File::Copy and others. no reinventing the wheel here, we're just making it cute. π
π EXPORTED FUNCTIONS π
the main characters. one by one. let's go. π«Ά
is_handle($var)β check if argument is a file handle- Parameters: some variable
- Returns:
1if it's file handle orundefotherwise
vibe check on your variable. does she handle files or does she not:
if (is_handle($var)) {
reset_handle($fh);
}
reset_handle($fh)β reset file handle- Parameters: file handle
- Returns: nothing
this function tries to set the filehandle to the begin of file and set binmode on it. like hitting rewind on the playlist π
my $fh = file_open('/etc/passwd');
...
do something with file
...
reset_handle($fh); # We can read it from the beginning
yes bestie, that comment is the entire point β after resetting you can read it from the beginning again. periodt.
file_open($file)β open file- Parameters: file name or file handle
- Returns: file handle
this function provides unified API for opening files. one function, every file, no drama. π
my $f = file_open('/etc/passwd');
file_read($file)β read file to scalar- Parameters: file name or file handle
- Returns: scalar content of file
this function provides ability to read file content to scalar variable. whole file, one scalar, slay. πβ¨
my $data = file_read('/etc/passwd');
print "Passwords file: $data\n";
file_write($file, $data)β write scalar data to file- Parameters: file name or open file handle
- Returns: length of written data or
undefin case of error
my $data = 'This should be file';
file_write('/tmp/file.dat', $data);
she tells you exactly how many characters she wrote. transparency queen. π
file_copy($in_file, $out_file)β copy file- Parameters: input file name, output file name
- Returns:
this function copy file to new location. two of everything now, bestie β the original and her clone. π§
file_move($in_file, $out_file)β move file- Parameters: input file name, output file name
- Returns:
1orundef
this function moves old file to new location. she's not duplicated, she's relocated. π
file_temp($dir)β create temporary file
Creates new temp file and return its handle. little baby file, here for a good time not a long time. πΌ
dir_create($dir)β create directory with parents- Parameters: directory name
- Returns: directory name or
undef
# Will create all parent catalogs if necessary
dir_create('/var/log/NetSDS/xxx');
she builds the whole family tree of directories, not just the leaf. generational wealth but for folders. π³π
dir_delete($dir)β remove directory recursive- Parameters: directory name
- Returns: dir name or
undefif error
print "We need no libs!";
dir_delete('/usr/lib');
β οΈ bestie this is recursive and that example is literally deleting /usr/lib. read it twice. scream once. π
dir_read($dir, $ext)β read files list from catalog- Parameters: directory name, extension of files to read
- Returns: list of files in catalog
my @logs = @{ dir_read('/var/log/httpd', 'log') };
print "Logs are: " . join (', ', @logs);
note the @{ ... } β she hands you back an arrayref, so deref it. iykyk. β¨
dir_read_recursive($dir, $ext, [$res])β read all files list recursive- Parameters:
$startcatalog,$extension - Returns: list of files with extension from parameters
- Parameters:
she goes deep. every subdirectory, every corner, all the files with that extension. π΅οΈββοΈ
my $tpls = dir_read_recursive('/etc/NetSDS', 'tmpl');
foreach my $tpl (@$tpls) {
pritn "Template: $tpl\n";
}
(yes, pritn is right there in the docs and no, i will not be fixing her typo, i respect her too much π)
exec_external($prog, [$param1, ... $paramN])β execute external program- Parameters: pragram name, arguments list (see
perldoc -f system) - Returns:
1if ok,undefotherwise
- Parameters: pragram name, arguments list (see
this function calls system() with given parameters and returns 1 if everything happened correctly (program executed and returned correct result). π
if (exec_external('/bin/rm', '-rf', '/')) {
print "Hey! We removed the world!";
}
β οΈ THAT EXAMPLE./bin/rm -rf /. that is not a drill bestie, that's the whole filesystem. also remember she's wrappingsystem()so the args go in as a list β go readperldoc -f systemlike she told you. π
π EXAMPLES π
None yet
ngl, zero examples. she's mysterious like that. π
π BUGS π
Unknown yet
no known bugs. we're choosing to believe her. π«Ά
π SEE ALSO π
go hang out with the rest of the friend group: IO::Handle, IO::Scalar, IO::File, File::Spec, File::Copy, File::Path, system()
π― TODO π―
- Implement more detailed error handling
one item on the vision board. we love a girl with goals. π
π AUTHOR π
Valentyn Solomko <pere@pere.org.ua>
Michael Bochkaryov <misha@rattler.kiev.ua>
and that's the whole page, bestie!! πβ¨ this was perl v5.36.0, dated 2022-11-20, straight out of the User Contributed Perl Documentation. read your docs, stay recursive, and please never rm -rf / β mwah π
ππΈ
π¦ where this page comes from
- debian bookworm: libnetsds-util-perl 1.045-2 Β· GPL-2.0-or-later
- debian trixie: libnetsds-util-perl 1.045-2 Β· GPL-2.0-or-later
- ubuntu noble: libnetsds-util-perl 1.045-2 Β· GPL-2.0-or-later
- ubuntu questing: libnetsds-util-perl 1.045-2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms