Prima::Utils(3)
โจ Prima::Utils โ miscellaneous routines ๐
okay so bestie, this is the Prima::Utils(3) page from the User Contributed Perl Documentation, and it is literally a little grab-bag of miscellaneous helper routines. group-chat energy on, facts intact on ๐
๐ธ NAME ๐ธ
Prima::Utils โ miscellaneous routines โจ
๐ซถ DESCRIPTION ๐ซถ
The module contains miscellaneous helper routines. thatโs the whole vibe of the description, but it is so real ๐
๐ฅ API ๐ฅ
alarm $TIMEOUT, $SUB, @PARAMSโ callsSUBwithPARAMSafterTIMEOUTmilliseconds. Returns0on failure, and the active timer on success. The timer can be stopped to disarm the alarm. โฐbeep [ FLAGS = mb::Error ]โ invokes the system-depended sound and/or visual bell, corresponding to one of these constants:
mb::Error
mb::Warning
mb::Information
mb::Question
get_guiโ returns one of thegui::XXXconstants that report the graphic user interface used in the system:
gui::Default
gui::Windows
gui::XLib
gui::GTK
get_osโ returns one of theapc::XXXconstants that report the system platform. Currently, the supported platforms are:
apc::Win32
apc::Unix
find_image PATHโ convertsPATHfrom a perl module notation into a file path and searches for the file in the@INCpaths set. If the file is found, its full filename is returned; otherwiseundefis returned. ๐last_errorโ returns the last system error, if any. ๐ญnearest_i NUMBERSโ performs thefloor($_ + .5)operation overNUMBERS, which can be an array or an arrayref. Returns converted integers in either an array or an arrayref form, depending on the calling syntax. ๐ขnearest_d NUMBERSโ performs thefloor($_ * 1e15 + .5) / 1e15operation overNUMBERS, which can be an array or an arrayref. Returns converted NVs in either an array or an arrayref form, depending on the calling syntax. Itโs used to protect against perl configurations that calculatesin,cosetc with only 15 significant digits in the mantissa. This function prevents the accumulation of error in these configurations. ๐งฎpath [ FILE ]โ if called with no parameters, returns the path to a directory, usually~/.prima, that can be used to store the user settings of a toolkit module or a program. IfFILEis specified, appends it to the path and returns the full file name. In the latter case, the path is automatically created byFile::Path::mkpathunless it already exists. ๐๏ธpost $SUB, @PARAMSโ postpones a call toSUBwithPARAMSuntil the next event loop tick. โณquery_drives_map [ FIRST_DRIVE = "A:" ]โ returns an anonymous array to drive letters used by the system.FIRST_DRIVEcan be set to another value to start enumeration from. Win32 can probe removable drives there, so to increase the responsiveness of the function it might be reasonable to call it withFIRST_DRIVEset to"C:".
If the system supports no drive letters, an empty array reference is returned (unix). ๐พ
query_drive_type DRIVEโ returns one of thedt::XXXconstants that describe the type of a drive, whereDRIVEis a 1-character string. If there is no such drive, or the system supports no drive letters (unix),dt::Noneis returned:
dt::None
dt::Unknown
dt::Floppy
dt::HDD
dt::Network
dt::CDROM
dt::Memory
sleep SECONDSโ same as perlโs nativesleep(i.e.CORE::sleep) but with the event loop running. Note that the argument it takes is seconds, for the sake of compatibility, while the rest of the toolkit operates in milliseconds. ๐ดusernameโ returns the login name of the user. Sometimes it is preferred to the perl-providedgetlogin(see"getlogin" in perlfunc). ๐ซถxcolor COLORโ accepts aCOLORstring in one of the three formats:
#rgb
#rrggbb
#rrrgggbbb
and returns a 24-bit RGB integer value. ๐
wait CONDITION [, TIMEOUT ]โ waits for a condition for maxTIMEOUTmilliseconds, or forever ifTIMEOUTis undefined.
Returns undef on failure, 0 on TIMEOUT, 1 on a successful CONDITION.
CONDITION is either a scalar reference, or a sub to be polled, where their values are treated as 0 as a signal to continue the waiting, and 1 as a stop signal. โฑ๏ธ
๐ฆ Unicode-aware filesystem functions ๐ฆ
Since perlโs win32 unicode support for files is unexistent, Prima has its own parallel set of functions mimicking native functions, ie open, chdir etc. This means that files with names that cannot be converted to ANSI (ie user-preferred) codepage are not visible in perl, but the functions below mitigate that problem. ๐ซก
The following fine points need to be understood before using these functions:
- Prima makes a distinction between whether scalars have their utf8 bit set or not throughout the whole toolkit. For example, text output in both unix and windows is different depending on the bit, treating non-utf8-bit text as locale-specific, and utf8-bit text as unicode. The same model is applied to file names. ๐ง
- Perl implementation for native Win32 creates virtual environments for each thread and may keep more than one instance of the current directory, environment variables, etc. This means that under Win32, calling
Prima::Utils::chdirwill NOT automatically makeCORE::chdirassume that value, even if the path is convertible to ANSI. Keep that in mind when mixing Prima and core functions. To add more confusion, under the unix, these two chdir calls are identical when the path is fully convertible. ๐ - Under unix, reading entries from the environment or the file system is opportunistic: if a text string (file name, environment entry) is a valid utf8 string, then it is treated and reported as one. Mostly because the
.UTF-8locales are default and found everywhere. Note that Prima ignores$ENV{LANG}here. This is a bit problematic on Perls under5.22as these donโt provide > means to check for the utf8 string validity, so every string will be slapped > a utf8 bit on here -- beware. ๐ - Setting environment variables may or may not sync with
%ENV, depending on how perl is built. Also,%ENVwill warn when trying to set scalars with utf-8 bit there. โ ๏ธ access PATH, MODEโ same asPOSIX::access.chdir DIRโ same asCORE::chdirbut disregards the thread-local environment on Win32.chmod PATH, MODEโ same asCORE::chmod.closedir, readdir, rewinddir, seekdir, telldir DIRHANDLEโ mimic the homonymous perl functions. ๐getcwdโ same asCwd::getcwd.getdir PATHโ reads the content of thePATHdirectory and returns an array of string pairs where the first item is a file name and the second is a file type.
The file type is a string, one of the following:
"fifo" - named pipe
"chr" - character special file
"dir" - directory
"blk" - block special file
"reg" - regular file
"lnk" - symbolic link
"sock" - socket
"wht" - whiteout
This function was implemented for faster directory reading, to avoid successive calls of stat for every file.
Also, getdir is consistently inclined to treat filenames in utf8, disregarding both perl unicode settings and the locale. ๐๏ธ
getenv NAMEโ reads directly from the environment, possibly bypassing%ENV, and disregarding the thread-local environment on Win32. ๐ฟlink OLDNAME, NEWNAMEโ same asCORE::link.local2sv TEXTโ converts 8-bit text into either an 8-bit non-utf8-bit or unicode utf8-bit string. May returnundefon memory allocation failure. ๐งตmkdir DIR, [ MODE = 0666 ]โ same asCORE::mkdir.open_file PATH, FLAGSโ same asPOSIX::open.open_dir PATHโ returns a directory handle to be used onreaddir,closedir,rewinddir,telldir,seekdir. ๐rename OLDNAME, NEWNAMEโ same asCORE::rename.rmdir PATHโ same asCORE::rmdir.setenv NAME, VALโ directly sets an environment variable, possibly bypassing%ENV, depending on how perl is built. Also disregards the thread-local environment on Win32.
Note that effective synchronization between this call and %ENV is not always possible, since the Win32 perl implementation simply does not allow that. One is advised to assign to %ENV manually, but only if both NAME and VAL donโt have their utf8 bit set, otherwise perl will warn about wide characters. ๐งผ
stat PATHโ same asCORE::stat, except on systems that provide sub-second time resolution, in which case returns the atime/mtime/ctime entries as floats, the same asTime::HiRes::statdoes. ๐sv2local TEXT, FAIL_IF_CANNOT = 1โ converts either an 8-bit non-utf8-bit or unicode utf8-bit string into a local encoding. May returnundefon memory allocation failure, or ifTEXTcontains unconvertible characters whenFAIL_IF_CANNOT = 1. ๐ชunlink PATHโ same asCORE::unlink.utime ATIME, MTIME, PATHโ same asCORE::utime, except on systems that provide sub-second time resolution, in which case returns the atime/mtime/ctime entries as floats, the same asTime::HiRes::utimedoes. โฐ
๐ AUTHOR ๐
Dmitry Karasik, <dmitry@karasik.eu.org>. ๐ซถ
๐ SEE ALSO ๐
Prima, Prima::sys::FS.
perl v5.42.0 ยท 2024-08-20 ยท Prima::Utils(3) โ thatโs the footer, bestie, byeee ๐
๐ฆ where this page comes from
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 ยท AGPL-3.0-only AND BSD-2-Clause
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 ยท AGPL-3.0-only AND BSD-2-Clause
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 ยท AGPL-3.0-only AND BSD-2-Clause
- opensuse-leap 16.0: perl-Prima 1.74000-bp160.1.13 ยท AGPL-3.0-only AND BSD-2-Clause
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms