#!/usr/bin/perl =head1 NAME B - Human-readable print of Unix PATH variable =head1 SYNOPSIS hpath [name] Options: name Name of an environment variable =head1 DESCRIPTION Prints each item of a path-type environment variable onto its own line. Useful for displaying PATH variable, for example. A path-type variable is really any variable which is a colon-separated list. Takes as input an environment variable name. Input need not be all upper case. Input need not be the full variable name. However, the abbreviated input must allow for "PATH" or "_PATH" to be appended to it. For example, "man", "MAN", "manpath" and "MANPATH" will all resolve to "MANPATH". If no input is given, the default is "PATH". Output is STDOUT. To promote laziness, -'s will be converted to _'s. This allows you to use "ld-library" to print out LD_LIBRARY_PATH. =head1 EXAMPLES Print the value of the PATH environment variable, one directory per line: hpath Print the value of the MANPATH environment variable: hpath man Print the value of the LD_LIBRARY_PATH environment variable: hpath ld-library =cut use strict; use warnings; my $name = (@ARGV) ? uc shift : 'PATH'; # If environment variable does not exist, try appending "PATH" to its name: my @attempts = ($name, "${name}PATH", "${name}_PATH"); # If that still won't work, try converting -'s to _'s if ($name =~ /-/) { push @attempts, map { (my $n = $_) =~ s/-/_/g; $n } @attempts; } my $path_var; for (@attempts) { if (exists $ENV{$_}) { $path_var = $ENV{$_}; last; } } # if THAT still doesn't work, try case-insensitively. if (not defined $path_var) { for my $a (@attempts) { my @matches = grep { lc $a eq lc } keys %ENV; if (@matches == 1) { $path_var = $ENV{$matches[0]}; last; } elsif (@matches > 1) { # err... which one? Probably should check if one # matches the input exactly, but we'll leave that as # an excersise for the reader :-) last; } } } if (defined $path_var) { my @list = split /:/, $path_var; print "$_\n" for @list; }