Prima::Utils(3)

๐Ÿ“ฆ
version 1.74000 ยท 5 versions
, in opensuse-leap 16.0

โœจ 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 โ€” calls SUB with PARAMS after TIMEOUT milliseconds. Returns 0 on 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 the gui::XXX constants that report the graphic user interface used in the system:
gui::Default
gui::Windows
gui::XLib
gui::GTK
  • get_os โ€” returns one of the apc::XXX constants that report the system platform. Currently, the supported platforms are:
apc::Win32
apc::Unix
  • find_image PATH โ€” converts PATH from a perl module notation into a file path and searches for the file in the @INC paths set. If the file is found, its full filename is returned; otherwise undef is returned. ๐Ÿ”Ž
  • last_error โ€” returns the last system error, if any. ๐Ÿ˜ญ
  • nearest_i NUMBERS โ€” performs the floor($_ + .5) operation over NUMBERS, 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 the floor($_ * 1e15 + .5) / 1e15 operation over NUMBERS, 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 calculate sin, cos etc 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. If FILE is specified, appends it to the path and returns the full file name. In the latter case, the path is automatically created by File::Path::mkpath unless it already exists. ๐Ÿ—‚๏ธ
  • post $SUB, @PARAMS โ€” postpones a call to SUB with PARAMS until the next event loop tick. โณ
  • query_drives_map [ FIRST_DRIVE = "A:" ] โ€” returns an anonymous array to drive letters used by the system. FIRST_DRIVE can 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 with FIRST_DRIVE set to "C:".

If the system supports no drive letters, an empty array reference is returned (unix). ๐Ÿ’พ

  • query_drive_type DRIVE โ€” returns one of the dt::XXX constants that describe the type of a drive, where DRIVE is a 1-character string. If there is no such drive, or the system supports no drive letters (unix), dt::None is returned:
dt::None
dt::Unknown
dt::Floppy
dt::HDD
dt::Network
dt::CDROM
dt::Memory
  • sleep SECONDS โ€” same as perlโ€™s native sleep (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-provided getlogin (see "getlogin" in perlfunc). ๐Ÿซถ
  • xcolor COLOR โ€” accepts a COLOR string 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 max TIMEOUT milliseconds, or forever if TIMEOUT is 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::chdir will NOT automatically make CORE::chdir assume 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-8 locales are default and found everywhere. Note that Prima ignores $ENV{LANG} here. This is a bit problematic on Perls under 5.22 as 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, %ENV will warn when trying to set scalars with utf-8 bit there. โš ๏ธ
  • access PATH, MODE โ€” same as POSIX::access.
  • chdir DIR โ€” same as CORE::chdir but disregards the thread-local environment on Win32.
  • chmod PATH, MODE โ€” same as CORE::chmod.
  • closedir, readdir, rewinddir, seekdir, telldir DIRHANDLE โ€” mimic the homonymous perl functions. ๐Ÿ“
  • getcwd โ€” same as Cwd::getcwd.
  • getdir PATH โ€” reads the content of the PATH directory 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 as CORE::link.
  • local2sv TEXT โ€” converts 8-bit text into either an 8-bit non-utf8-bit or unicode utf8-bit string. May return undef on memory allocation failure. ๐Ÿงต
  • mkdir DIR, [ MODE = 0666 ] โ€” same as CORE::mkdir.
  • open_file PATH, FLAGS โ€” same as POSIX::open.
  • open_dir PATH โ€” returns a directory handle to be used on readdir, closedir, rewinddir, telldir, seekdir. ๐Ÿ“‚
  • rename OLDNAME, NEWNAME โ€” same as CORE::rename.
  • rmdir PATH โ€” same as CORE::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 as CORE::stat, except on systems that provide sub-second time resolution, in which case returns the atime/mtime/ctime entries as floats, the same as Time::HiRes::stat does. ๐Ÿ“Š
  • sv2local TEXT, FAIL_IF_CANNOT = 1 โ€” converts either an 8-bit non-utf8-bit or unicode utf8-bit string into a local encoding. May return undef on memory allocation failure, or if TEXT contains unconvertible characters when FAIL_IF_CANNOT = 1. ๐Ÿช„
  • unlink PATH โ€” same as CORE::unlink.
  • utime ATIME, MTIME, PATH โ€” same as CORE::utime, except on systems that provide sub-second time resolution, in which case returns the atime/mtime/ctime entries as floats, the same as Time::HiRes::utime does. โฐ

๐Ÿ‘‘ 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