NAME
Nobody::Util - Utilities Nobody Uses
SYNOPSIS
use Nobody::Util;
my $id = file_id("/etc/passwd");
my $date = serdate();
my @vers = vsort(qw( 1.10 1.9 2.0 1.1 ));
DESCRIPTION
Nobody::Util is a collection of utility functions accumulated over years of Perl development by Rich Paul (CPAN: NOBODY). It is, as the name suggests, full of things that nobody else does — but that save time.
For compatibility, this module currently delegates imports to Nobody::Dev, providing the same broad namespace for existing scripts. Explicit imports select only the requested symbols:
use Nobody::Util qw(vcmp partition);
use List::Util qw(sum);
use Nobody::Path qw(path);
use FindBin qw($Bin);
EXPORTED BY DEFAULT
All functions listed under "FUNCTIONS" are exported by default.
EXPORT_OK / EXPORT_TAGS
use Nobody::Util qw( :all ); # full Dev namespace for compatibility
Dependency functions and FindBin variables are available through the compatibility importer. The utility implementation and its own export list remain here; Nobody::Dev owns the broad namespace. use Nobody::Util () loads the utilities without invoking the compatibility importer.
FUNCTIONS
File Descriptor Utilities
- open_fds( [$resolve] )
-
Returns a list of open file descriptor numbers for the current process. If
$resolveis true, returns a flat list offd => pathpairs. - getfds()
-
Alias for
open_fds; excludes the directory descriptor used internally. - getfl( $fh )
-
Returns the file status flags for
$fhviafcntl(F_GETFL). - setfl( $fh, $flags )
-
Sets the file status flags for
$fhviafcntl(F_SETFL). - nonblock( $fh )
-
Sets
O_NONBLOCKon$fh.
Object / Reference Utilities
- class( $thing )
-
Returns
ref($thing)if it is a reference,$thingitself if it is a plain string (class name), or'undef'if it is undefined. - safe_isa( $obj, $class )
-
Returns true if
$objis a blessed reference thatisa$class. Returnsundefrather than dying if$objis not a reference. - safe_blessed( $obj )
-
Returns the class name if
$objis blessed,undefotherwise. - safe_can( $obj, $method )
-
Returns the method coderef if
$objis blessed and can$method,undefotherwise. - mkref( $thing )
-
Returns
$thingif it is already a reference, otherwise\$thing. - flatten( @list )
-
Recursively flattens arrays and hashes in
@listinto a flat list.
String / List Utilities
- pad( @strings )
-
Right-pads all strings to the length of the longest, using
.as fill. - pasteLines( @strings )
-
Joins strings, removing line-continuation backslashes.
- lsort( @list )
-
Sorts by length first, then lexicographically.
- lcmp
-
Sort comparator for
lsort(length then lex).
Version Comparison
- vcmp( $a, $b )
-
Compares two version strings numerically segment by segment.
- vsort( @versions )
-
Sorts a list of version strings using
vcmp.
Date / Time
- serdate( [$time] )
-
Returns a sortable timestamp string in
YYYYMMDD-HHMMSSformat (UTC). Defaults to the current time.
Process Utilities
File Utilities
- file_id( $path )
-
Returns a unique string identifier for a file based on its device and inode numbers, suitable for detecting hard links.
- uri( $string )
-
Constructs and returns a
URIobject.URIis loaded on demand. - serial_maker( %args )
-
Returns a closure that generates sequentially-numbered unique files or directories. Arguments:
fmt(sprintf format, required),min(default 0),max(default 1000),dir(make directories if true).
Recovered Helpers
- chomped( @strings )
-
Returns chomped copies of the supplied strings.
- partition( \@predicates, @objects )
-
Returns one array reference per predicate, followed by an unmatched bucket. Each object goes into the first bucket whose predicate returns true; later predicates are skipped. Predicates receive the object as both
$_and$_[0]. Buckets preserve input order and exist even when empty. With no predicates, all objects go into the single unmatched bucket. - grep2 { predicate } @array
-
Returns matching elements and replaces the source array with nonmatching ones, preserving order within both groups. The predicate uses
$_. - args( @arguments )
-
Returns a hash reference mapping keys to arrays of supplied values. Nested array and hash references expand into argument lists. Repeated keys append values; an unpaired key is an error.
- ref_cnt( @references ), refcount( @references )
-
Returns a hash reference counting reference types (or blessed class names). Nonreferences are an error.
refcountis an alias; this does not measure Perl's internal reference counts. - newer( $file, @others ), older( $file, @others )
-
True when the first file's modification time is newer or older than at least one of the other files. Equal times do not match. A failed stat is an error.
- xcaller( [$depth] )
-
Returns an independent hash reference describing the caller at the requested depth (default zero), or
undefbeyond the call stack. Keys arepkg,file,line,sub,hasargs,wantarray,evaltext,is_require,hints,bitmask, andhinthash. - exports( [$package] )
-
Returns package names mapped to their
EXPORT,EXPORT_OK, andEXPORT_TAGSreferences: a list in list context, a hash reference in scalar context. Without an argument, inspects module names in%INCwithout loading additional modules.
Introspection
- deparse( $coderef )
-
Returns a string representation of
$coderefusingB::Deparse. - methods( $obj_or_class )
-
Prints all methods available to
$obj_or_class, including inherited ones. - methods_via( $class, $prefix, $crawl_upward )
-
Worker for
methods(); crawls the@ISAtree. - print_methods( $obj_or_class )
-
Like
methods()but pretty-prints viaddx.
AUTHOR
Rich Paul, <nobody at cpan.org>
LICENSE
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.