Regex 浏览目录中的文件列表

Regex 浏览目录中的文件列表,regex,perl,Regex,Perl,我想写一个Perl脚本,它接收一个文件和一个目录的路径, 该目录包含指向其他目录的链接文件。 我想把目录里的文件一个一个地看一遍, 对于每个文件,在我的输入文件中搜索文件名,并将其替换为链接到的地址 例如,如果脚本收到目录“/myworkspace/mydir” 其中包含以下文件: myfile1 -> /myworkspace/globaldir/file1 myfile2 -> /myworkspace/somedir/file2 myfile3 -> /glo

我想写一个Perl脚本,它接收一个文件和一个目录的路径, 该目录包含指向其他目录的链接文件。 我想把目录里的文件一个一个地看一遍, 对于每个文件,在我的输入文件中搜索文件名,并将其替换为链接到的地址

例如,如果脚本收到目录
“/myworkspace/mydir”
其中包含以下文件:

myfile1  ->  /myworkspace/globaldir/file1
myfile2  ->  /myworkspace/somedir/file2
myfile3  ->  /globalworkspace/file3
对于以下输入文件:

 " cd /myworkspace/mydir/myfile1   
   cp -r /myworkspace/mydir/myfile2 /myworkspace/mydir/myfile3 "
我希望获得以下输出:

" cd /myworkspace/globaldir/file1
  cp -r /myworkspace/somedir/file2 /globalworkspace/file3 "
什么是一种有效的方法呢?

我曾经处理绝对路径和路径连接

#!/usr/bin/perl
use warnings;
use strict;

use Path::Tiny;

my $dir   = shift;
my $input = shift;

my %replace;

opendir my $DIR, $dir or die $!;
while (my $file = readdir $DIR) {
    $file = path($dir)->child($file)->absolute;
    $replace{$file} = readlink $file if -l $file;
}
close $DIR;

my $regex = join '|',
            map quotemeta,                   # To handle filenames containing "." etc.
            sort { length $b <=> length $a } # Not to replace parts of paths (process longer first).
            keys %replace;

open my $IN, '<', $input or die $!;
while (<$IN>) {
    s/($regex)/$replace{$1}/g;
    print
}
#/usr/bin/perl
使用警告;
严格使用;
使用路径::微小;
我的$dir=shift;
我的$input=shift;
我的%replace;
opendir my$DIR、$DIR或die$!;
while(my$file=readdir$DIR){
$file=path($dir)->child($file)->absolute;
$replace{$file}=readlink$file如果-l$file;
}
关闭$DIR;
my$regex=加入“|”,
映射quotemeta,#以处理包含“.”等的文件名。
排序{length$b length$a}#不替换部分路径(先处理更长的路径)。
钥匙%更换;

打开我的$IN,“
文件::查找”
是实现此目的的工具job@Sobrique是吗?我没有看到涉及到任何子目录。