如何解决“可能在正则表达式中对$ \进行意外的内插”?

时间:2018-12-21 14:07:01

标签: regex perl

在工作中,我有这个旧脚本,其作者不再在公司工作。脚本本身运行正常,但是每次运行时,都会收到以下警告:

Possible unintended interpolation of $\ in regex at /bin/script line 223.
Possible unintended interpolation of $\ in regex at /bin/script line 226.

我标记的两行是对应的:

sub get_modul {

...

    open (IN, "path/to/file") || Pl_Fatal ("Error trying to open file");
    $tempvar = <IN>;
    close (IN);
    @temp = split(":", $tempvar);
    if ($#temp != 1) {
       $MODUL =~ s/$\$2\///;   # <--
    }
    else {
        $MODUL =~ s/$\$0\///;  # <--
    }
}

当我收到两行互相排斥的警告时,我认为它不必对输入做任何事情,只是以防万一:

脚本分析:server:user@host:/path/to/rep/path/to/rep,两种情况下的输出均为/path/to/rep

如果有任何帮助,我正在运行Perl v5.18.2。

1 个答案:

答案 0 :(得分:2)

此警告在perldiag中进行了解释。

  

在正则表达式中可能会意外地插入$ \

     

(不明确),您在正则表达式中说了类似export class ScheduleManager extends SampleBase { constructor() { super(...arguments); this.state = { visible : false } } openModal() { this.setState({ visible : true }); } closeModal() { this.setState({ visible : false }); } on_render_cell(args) { let ele = document.createElement('div'); var sum=0; var cursor=Event.find({Teacher:teacherName}); cursor.forEach(function(event){ if(convert(event.StartTime)===convert(args.date)) sum = sum + parseFloat(event.Duration); }); let hoursPerDay = sum/60; let percentHours=(hoursPerDay/maxHoursPerDay)*100; let result =''; if(percentHours===0) { result='<img id="aaa" ref="abc" className="weather-image" height="25px" width="25px" src="http://www.clker.com/cliparts/h/e/s/t/j/U/grey-circle.svg.thumb.png" onClick={console.log(this)} />'; } else if(percentHours<=25) { result = '<img className="weather-image" src= "http://www.clker.com/cliparts/0/w/P/n/G/3/blue-circle-th.png" />'; } else if(percentHours>25 && percentHours<=50) { result='<img className="weather-image" src= "http://www.clker.com/cliparts/o/b/y/x/Z/c/yellow-dot-th.png" />'; } else if(percentHours>50 && percentHours<=75) { result = '<img className="weather-image" src= "http://www.clker.com/cliparts/1/A/W/q/h/h/orange-circle-th.png" />'; } else { result='<img className="weather-image" src= "http://www.clker.com/cliparts/3/m/Q/u/P/J/red-circle-small-new.svg.thumb.png" />'; } ele.innerHTML = result; (args.element).appendChild(ele.firstChild); } render(){ return ( <div> <input type="button" value="Open" onClick={() => this.openModal()} /> <Modal visible={this.state.visible} width="400" height="300" effect="fadeInUp" onClickAway={() => this.closeModal()} > <div> <h1>Title</h1> <p>Some Contents</p> <a href="javascript:void(0);" onClick={() => this.closeModal()}>Close</a> </div> </Modal> <p>Grey circle denotes no classes for the day</p> <p>Blue circle denotes less than 25% occupancy for the day</p> ) } } 的内容。正则表达式m/$\/转换为:匹配单词'foo',输出记录分隔符(请参阅perlvar中的$ \)和字母's'(一次或多次),后跟单词'bar'。      

如果这是您想要的,则可以使用m/foo$\s+bar/m(例如:m/${\}/)使警告消失。

     

如果相反,您打算在行末匹配单词“ foo”,然后在下一行匹配空格,然后在下一行匹配单词“ bar”,则可以使用m/foo${\}s+bar/(例如:{{1} }。

m/$(?)\/是输出记录分隔符。这就是Perl自动将其附加到使用m/foo$(?)\s+bar/输出的任何内容上。默认值为$\

因此,在您的正则表达式中,可能什么也不做(因为print()在字符串中使用时会变成空字符串)。但是无法确保没有看到其余的代码-因为您可能会在程序的其他位置更改undef的值。

相关问题