瀏覽代碼

Dpkg::Path: new function find_command() that finds a command on the PATH

Raphaël Hertzog 16 年之前
父節點
當前提交
12c8bc2c1d
共有 1 個文件被更改,包括 17 次插入2 次删除
  1. 17 2
      scripts/Dpkg/Path.pm

+ 17 - 2
scripts/Dpkg/Path.pm

@@ -18,14 +18,14 @@ package Dpkg::Path;
 use strict;
 use strict;
 use warnings;
 use warnings;
 
 
-our $VERSION = "1.00";
+our $VERSION = "1.01";
 
 
 use base qw(Exporter);
 use base qw(Exporter);
 use File::Spec;
 use File::Spec;
 use Cwd qw(realpath);
 use Cwd qw(realpath);
 our @EXPORT_OK = qw(get_pkg_root_dir relative_to_pkg_root
 our @EXPORT_OK = qw(get_pkg_root_dir relative_to_pkg_root
 		    guess_pkg_root_dir check_files_are_the_same
 		    guess_pkg_root_dir check_files_are_the_same
-		    resolve_symlink canonpath);
+		    resolve_symlink canonpath find_command);
 
 
 =encoding utf8
 =encoding utf8
 
 
@@ -190,6 +190,21 @@ sub resolve_symlink($) {
     }
     }
 }
 }
 
 
+
+=item my $cmdpath = find_command($command)
+
+Return the path of the command if available on the $PATH, undef otherwise.
+
+=cut
+
+sub find_command($) {
+    my $cmd = shift;
+    foreach my $dir (split(/:/, $ENV{'PATH'})) {
+	return "$dir/$cmd" if -x "$dir/$cmd";
+    }
+    return undef;
+}
+
 =back
 =back
 
 
 =head1 AUTHOR
 =head1 AUTHOR