Programming Tips - Perl: dirname() and basename()

Date: 2018nov6 Language: perl Q. Perl: dirname() and basename() A. There are library functions for this but maybe you don't want to include them.
sub dirname($) { my($orig) = @_; my($a) = $orig; $a =~ s?(/|\\)$??; # Remove trailing slash (or backslash) $a =~ s?(/|\\)[^(/|\\)]*$??; # Remove trailing slash (or backslash) and basename if ($orig eq $a) { return '.'; } return $a; } sub basename($) { my($a) = @_; $a =~ s?(/|\\)$??; # Remove trailing slash (or backslash) if ($a =~ m/([^\/\\]*)$/) { # Match trailing non slash (or backslash) $a = $1; } return $a; }