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 $resolve is true, returns a flat list of fd => path pairs.

getfds()

Alias for open_fds; excludes the directory descriptor used internally.

getfl( $fh )

Returns the file status flags for $fh via fcntl(F_GETFL).

setfl( $fh, $flags )

Sets the file status flags for $fh via fcntl(F_SETFL).

nonblock( $fh )

Sets O_NONBLOCK on $fh.

Object / Reference Utilities

class( $thing )

Returns ref($thing) if it is a reference, $thing itself if it is a plain string (class name), or 'undef' if it is undefined.

safe_isa( $obj, $class )

Returns true if $obj is a blessed reference that isa $class. Returns undef rather than dying if $obj is not a reference.

safe_blessed( $obj )

Returns the class name if $obj is blessed, undef otherwise.

safe_can( $obj, $method )

Returns the method coderef if $obj is blessed and can $method, undef otherwise.

mkref( $thing )

Returns $thing if it is already a reference, otherwise \$thing.

flatten( @list )

Recursively flattens arrays and hashes in @list into 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-HHMMSS format (UTC). Defaults to the current time.

Process Utilities

child_wait()

Waits for all child processes to exit, warning on non-zero exit status.

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 URI object. URI is 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. refcount is 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 undef beyond the call stack. Keys are pkg, file, line, sub, hasargs, wantarray, evaltext, is_require, hints, bitmask, and hinthash.

exports( [$package] )

Returns package names mapped to their EXPORT, EXPORT_OK, and EXPORT_TAGS references: a list in list context, a hash reference in scalar context. Without an argument, inspects module names in %INC without loading additional modules.

Introspection

deparse( $coderef )

Returns a string representation of $coderef using B::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 @ISA tree.

Like methods() but pretty-prints via ddx.

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.