Path(3pm)

πŸ“¦
version 0.19 Β· 6 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

Env::Path β€” advanced operations on path variables, bestie. like, the ✨deluxe✨ way to mess with your PATH and friends.


πŸ¦‹ SYNOPSIS πŸ¦‹

okay so here's the whole vibe, straight up:

use Env::Path;

# basic usage
my $manpath = Env::Path->MANPATH;
$manpath->Append('/opt/samba/man');
for ($manpath->List) { print $_, "\n" };

# similar to above using the "implicit object" shorthand
Env::Path->MANPATH;
MANPATH->Append('/opt/samba/man');
for (MANPATH->List) { print $_, "\n" };

# one-shot use
Env::Path->PATH->Append('/usr/sbin');

# Windows-ish example
use Env::Path qw(PATH);
PATH->Append('C:\\Program Files\\Debugging Tools for Windows');
print "$_\n" for (PATH->List);

# change instances of /usr/local/bin to an architecture-specific dir
Env::Path->PATH->Replace('/usr/local/bin', "/usr/local/$ENV{PLATFORM}/bin");

# more complex use (different names for same semantics)
my $libpath;
if ($^O =~ /aix/) {
    $libpath = Env::Path->LIBPATH;
} else {
    $libpath = Env::Path->LD_LIBRARY_PATH;
}
$libpath->Assign(qw(/usr/lib /usr/openwin/lib));
$libpath->Prepend('/usr/ucblib') unless $libpath->Contains('/usr/ucblib');
$libpath->InsertAfter('/usr/ucblib', '/xx/yy/zz');
$libpath->Uniqify;
$libpath->DeleteNonexistent;
$libpath->Remove('/usr/local/lib');
print $libpath->Name, ":";
for ($libpath->List) { print " $_" };
print "\n";

# simplest usage: bless all existing EV's as Env::Path objects
use Env::Path ':all';
my @cats = PATH->Whence('cat*');
print "@cats\n";

no cap, that's the whole tour. 🫢


🌸 DESCRIPTION 🌸

so Env::Path gives you an object-oriented interface to path variables β€” and a path variable is defined as that subclass of environment variables which names an ordered list of filesystem elements, separated by a platform-standard separator (typically ':' on UNIX and ';' on Windows). periodt.

like, honestly? core Perl can already do this:

$ENV{PATH} .= ":/usr/local/bin";

and for most uses that absolutely slays. Env::Path is for the others β€” the cases where you need to insert or remove interior path entries, strip redundancies, operate on a pathvar without having to know whether the current platform uses ":" or ";", operate on a pathvar which may have a different name on different platforms, etc. it's giving "i got you, babe." πŸ’…

the OO interface is slightly unusual in that the environment variable is itself the object and the constructor is Env::Path->AUTOLOAD(); thus

Env::Path->MANPATH;

will bless $ENV{MANPATH} into its package while leaving it otherwise unmodified (with the exception of possible autovivification). unlike most objects, this is a scalar and thus can have only one attribute; its value. lowkey iconic, ngl.

in other words, Env::Path simply defines a set of methods a path variable may call on itself without changing the variable's value or other semantics. 🀯

also, while the object reference may be assigned and used in the normal style

my $path = Env::Path->CLASSPATH;
$path->Append('/opt/foo/classes.jar');

a shorthand is also available:

Env::Path->CLASSPATH;
CLASSPATH->Append('/opt/foo/classes.jar');

i.e. the name of the path variable may be used as a proxy for its object reference. this may be done at 'use' time too:

use Env::Path qw(PATH CLASSPATH);   # or qw(:all) to bless all EV's
CLASSPATH->Append('/opt/foo/classes.jar');

the design is intended to make use of this module as lightweight as possible. rather than creating a new object to manage an environment variable, the environment variable is provided a set of methods for self-modification but is otherwise left undisturbed and can be used in all normal ways. so like, it's that friend who hypes you up without changing who you are. πŸ’–

✨ CLASS METHODS ✨

  • <CONSTRUCTOR> β€” the constructor may have any name; it's assumed to name a path variable as defined above. returns the object reference.
  • PathSeparator β€” returns or sets the platform-specific path separator character, by default : on open platforms and ; on monopolistic ones. πŸ’€

πŸ’… INSTANCE METHODS πŸ’…

unless otherwise indicated, these methods return the object reference, allowing method calls to be strung together. all methods which take lists join them together using the value of Env::Path->PathSeparator. chain them like friendship bracelets. πŸŽ€

  • Name β€” returns the name of the pathvar.
  • Contains β€” returns true iff the specified entry is present in the pathvar.
  • Assign β€” takes a list and sets the pathvar to that value, separated by the current PathSeparator.
  • List β€” returns the current path in list format.
  • Prepend β€” for each entry in the supplied list, removes it from the pathvar if present and prepends it, thus ensuring that it's present exactly once and at the front. main character energy, honestly.
  • Append β€” analogous to Prepend.
  • InsertBefore β€” takes a <dirname> and a list, inserts the list just before the first instance of the <dirname>. if dirname is not found, works just like Prepend. as with Prepend, duplicates of the supplied entries are removed.
  • InsertAfter β€” analogous to InsertBefore
  • Remove β€” removes the specified entries from the path.
  • Replace β€” takes a /pattern/ and a list. traverses the path and replaces all entries which match the pattern with the concatenated list entries.
  • ListNonexistent β€” returns a list of all entries which do not exist as filesystem entities.
  • DeleteNonexistent β€” removes from the path all entries which do not exist as filesystem entities.
  • Uniqify β€” removes redundant entries (the 2nd through nth instances of each entry). decluttering queen. ✨
  • Whence β€” takes a pattern and returns an ordered list of all filenames found along the path which match it and are executable.
  • Shell β€” returns a string suitable for passing to a shell which would set and export the pathvar to its current value within the shell context.

🧚 NOTES 🧚

  • no provision is made for path variables which are not also environment variables, a situation which is technically possible but quite rare. sad, but true. 😭
  • except where necessary no assumption is made that path entries should be directories, because pathvars like CLASSPATH may contain "virtual dirs" such as zip/jar files. for instance the DeleteNonexistent method does not remove entries which are files. in Perl terms the test applied is "-e", not "-d". not -d. got it? got it.
  • the shorthand notation for pathvar FOO is implemented by hacking @FOO::ISA, so there's a slight risk of namespace collision if your code also creates packages with all-upper-case names. no packages are created unless the shorthand notation is employed. heads up bestie ⚠️
  • there's some cute code in the Env module by Gregor N. Purdy for splitting pathvars into arrays using ties. i'd love to be able to take advantage of that, and it pains me to do the same thing (and not as well) here rather than using Env. unfortunately it's a newish feature (5.6.0? 5.005? 5.6.1?) in Env and i don't want Env::Path to be "tied" to the very latest Perls. the way that i'm literally suffering for backwards compatibility 😭

🌈 WORKS ON 🌈

UNIX and Windows. that's it, that's the list. iconic coverage fr fr.


πŸ‘‘ AUTHOR πŸ‘‘

David Boyce <dsbperl AT boyski.com>


Copyright (c) 2000-2001 David Boyce. All rights reserved. this Perl program is free software; you may redistribute and/or modify it under the same terms as Perl itself.


πŸ“ SEE ALSO πŸ“

perl(1), perlobj(1), Env::Array(3), Env(3)


and that's a wrap, bestie!! πŸ¦‹βœ¨ this lil doc is perl v5.36.0, dated 2022-10-13, and it's Path(3pm) in the User Contributed Perl Documentation corner of the man pages. go forth and slay your path variables πŸ’–πŸ’…

πŸ“¦ where this page comes from