简体   繁体   English

如何在多个文件中更新 Perl 脚本中的路径

[英]How to update path in Perl script in multiple files

I am working on creating some training material where I am using perl.我正在创建一些我使用 perl 的培训材料。 One of the things I want to do is have the scripts be set up for the student correctly, regardless of where they extra the compressed files.我想做的一件事是为学生正确设置脚本,无论他们在哪里额外压缩文件。 I am working on a Windows batch file that will copy the perl templates to the working location and then update path in the copy of the perl template files to the correct location.我正在处理一个 Windows 批处理文件,它将 perl 模板复制到工作位置,然后将 perl 模板文件副本中的路径更新到正确的位置。 The perl template have this as the first line: perl 模板将此作为第一行:

#!_BASE_software/perl/bin/perl.exe

The batch file looks like this:批处理文件如下所示:

SET TRAINING=%~dp0
copy %TRAINING%\template\*.pl %TRAINING%work
%TRAINING%software\perl\bin\perl -pi.bak -e 's/_BASE_/%TRAINING%/g' %TRAINING%work\*.pl

I have a few problems with this:我对此有几个问题:

  1. Perl doesn't seem to like the wildcard in the filename Perl 似乎不喜欢文件名中的通配符
  2. It turns out that %TRAINING% is going to expand into a string with backslashes which need to be converted into forwardslashes and needs to be escaped within the regex.事实证明 %TRAINING% 将扩展为带有反斜杠的字符串,需要将其转换为正斜杠并需要在正则表达式中进行转义。

How do I fix this?我该如何解决?

First of all, Windows doesn't use the shebang line, so I'm not sure why you're doing any of this work in the first place.首先,Windows 不使用 shebang 行,所以我不确定你为什么要首先做这些工作。

Perl will read the shebang line and look for options if perl is found in the path, even on Windows, but that means that #!perl is sufficient if you want to pass options via the shebang line (eg #!perl -n ). Perl 将读取 shebang 行并在路径中找到perl查找选项,即使在 Windows 上也是如此,但这意味着如果您想通过 shebang 行(例如#!perl -n )传递选项, #!perl就足够了。

Now, it's possible that you use Cygwin, MSYS or some other unix emulation instead of Windows to run the program, but you are placing a Windows path in the shebang line ( C:... ) rather than a unix path, so that doesn't make sense either.现在,您可以使用 Cygwin、MSYS 或其他一些 unix 仿真而不是 Windows 来运行程序,但是您将 Windows 路径放置在 shebang 行( C:... )而不是 unix 路径中,这样就不会也没道理

There are three additional problems with the attempt:尝试还有三个额外的问题:

  1. cmd uses double-quotes for quoting. cmd使用双引号进行引用。
  2. cmd doesn't perform wildcard expansion like sh , so it's up to your program do it. cmd不像sh那样执行通配符扩展,所以这取决于你的程序。
  3. You are trying to generate Perl code from cmd .您正在尝试从cmd生成 Perl 代码。 ouch.哎哟。

If we go ahead, we get:如果我们继续,我们会得到:

"%TRAINING%software\perl\bin\perl" -MFile::DosGlob=glob -pe"BEGIN { @ARGV = map glob, @ARGV; $base = $ENV{TRAINING} =~ s{\\}{/}rg } s/_BASE_/$base/g" -i.bak -- %TRAINING%work\*.pl

If we add line breaks for readability, we get the following (that cmd won't accept):如果我们为可读性添加换行符,我们会得到以下结果(该cmd不会接受):

"%TRAINING%software\perl\bin\perl"
   -MFile::DosGlob=glob
   -pe"
      BEGIN {
         @ARGV = map glob, @ARGV;
         $base = $ENV{TRAINING} =~ s{\\}{/}rg
      }
      s/_BASE_/$base/g
   "
   -i.bak -- %TRAINING%work\*.pl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM