现在不再支持如何用备用$ * = 1替换$ * = 1

时间:2020-03-06 14:19:20  来源:igfitidea点击:

我是一名完全的perl新手,正在使用perl 5.10运行perl脚本并收到以下警告:

$* is no longer supported at migrate.pl line 380.

谁能描述$ *做了什么以及现在推荐的替换形式是什么?
或者,如果我们可以指出我的文档来描述这一点,那将是很棒的。

我正在运行的脚本是将源代码数据库从vss迁移到svn,可以在这里找到:
http://www.x2systems.com/files/migrate.pl.txt

使用它的两个代码段是:

$* = 1;
    $/ = ':';

    $cmd = $SSCMD . " Dir -I- \"$proj\"";
    $_ = `$cmd`;

    # what this next expression does is to merge wrapped lines like:
    #    $/DeviceAuthority/src/com/eclyptic/networkdevicedomain/deviceinterrogator/excep
    #    tion:
    # into:
    #    $/DeviceAuthority/src/com/eclyptic/networkdevicedomain/deviceinterrogator/exception:
    s/\n((\w*\-*\.*\w*\/*)+\:)//g;

    $* = 0;

然后一些方法:

$cmd = $SSCMD . " get -GTM -W -I-Y -GL\"$localdir\" -V$version \"$file\" 2>&1";
            $out = `$cmd`;

            # get rid of stupid VSS warning messages
            $* = 1;
            $out =~ s/\n?Project.*rebuilt\.//g;
            $out =~ s/\n?File.*rebuilt\.//g;
            $out =~ s/\n.*was moved out of this project.*rebuilt\.//g;
            $out =~ s/\nContinue anyway.*Y//g;
            $* = 0;

非常感谢,

  • 罗里

解决方案

打开多行模式。从perl 5.0(从1994年开始)开始,正确的方法是在正则表达式中添加m和/或者s修饰符,例如

s/\n?Project.*rebuilt\.//msg

从Perlvar的Perl 5.8版本开始:

Set to a non-zero integer value to do
  multi-line matching within a string
  [...] Use of $*  is deprecated in
  modern Perl, supplanted by the /s and
  /m modifiers on pattern matching.

尽管使用/ s和/ m更好,但是我们需要为每个正则表达式设置修饰符(适当!)。

perlvar还说:"此变量仅影响^和$的解释。"这给人的印象是,它仅等效于/ m,而不等效于/ s。

注意$$是一个全局变量。因为对它的更改不是使用local关键字进行本地化的,所以它将影响程序中的所有正则表达式,而不仅仅是影响块中跟随它的表达式。这将使正确更新脚本更加困难。

从perlvar:

Use of $* is deprecated in modern Perl, supplanted by the /s and /m modifiers on pattern matching.

如果我们可以访问要匹配的地方,只需将其添加到末尾即可:

$haystack =~ m/.../sm;

如果我们只能访问该字符串,则可以将表达式用

qr/(?ms-ix:$expr)/;

或者情况:

s/\n((\w*\-*\.*\w*\/*)+\:)//gsm;

基本上可以这样说,在后续的正则表达式(s ///或者m //)中,^或者$断言应该能够匹配嵌入在字符串中的换行符之前或者之后。

建议的等效项是正则表达式末尾的m修饰符(例如s / \ n((\ w -。* \ w * / *)+:)/ $ 1 / gm;)。

从perldoc perlvar:

$*
  
  Set to a non-zero integer value to do multi-line matching within a string, 0 (or undefined) to tell Perl that it can assume that strings contain a single line, for the purpose of optimizing pattern matches. Pattern matches on strings containing multiple newlines can produce confusing results when $* is 0 or undefined. Default is undefined. (Mnemonic: * matches multiple things.) This variable influences the interpretation of only ^ and $. A literal newline can be searched for even when $* == 0. 
  
  Use of $* is deprecated in modern Perl, supplanted by the /s and /m modifiers on pattern matching.
  
  Assigning a non-numerical value to $* triggers a warning (and makes $* act as if $* == 0), while assigning a numerical value to $* makes that an implicit int is applied on the value.