复杂的常规子表达式递归限制(32766)超过

时间:2015-12-10 11:07:15

标签: regex linux perl recursion

我有一个执行pgrep的perl脚本,如下所示:

use warnings;

my  @instance = `pgrep -fl varnish`;
foreach my $line (@instance) {
my $ip;
        my $port;
        my $pid;
        my $pidfile;
        my $fileconfiguration;
        my $bakendhost;
        my $bakendport;
        my $sharememory;
        my $user;

        chomp $line;
        if ($line =~ m/-T\s+([0-9]+.*)+:(\d+)\s+/) {
           $ip = $1;
           $port = $2;
        }
}

但是,在启动脚本时出现以下错误/警告:

  

复杂的常规子表达式递归限制(32766)超过   /home/k.sewnundun/test.pl第16行。

     

/home/k.sewnundun/test.pl行超出了复杂的常规子表达式递归限制(32766)   16。

请在下面找到调试:

Loading DB routines from perl5db.pl version 1.32
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(/home/k.sewnundun/test.pl:3):
3:      my  @instance = `pgrep -fl varnish`;
  DB<1> n
main::(/home/k.sewnundun/test.pl:4):
4:      foreach my $line (@instance) {
  DB<1> n
main::(/home/k.sewnundun/test.pl:5):
5:      my $ip;
  DB<1> print $line
3066 /opt/rh/varnish4/root/usr/sbin/varnishd -P /var/run-daemon/varnishd-toto4.pid -a 0.0.0.0:8080 -f /etc/varnish/etc/toto4/toto4-current.vcl -u varnish4-toto4 -g varnish4-toto4 -l 80M -T 0.0.0.0:38080 -p thread_pool_min=50 -p thread_pool_max=1000 -p thread_pool_timeout=120 -p vcl_dir=/etc/varnish/etc/toto4 -n /home/varnish/toto4/working -s malloc, -S /etc/varnish/etc/toto4/toto4.secret

  DB<2> n
main::(/home/k.sewnundun/test.pl:6):
6:              my $port;
  DB<2> n
main::(/home/k.sewnundun/test.pl:7):
7:              my $pid;
  DB<2> n
main::(/home/k.sewnundun/test.pl:8):
8:              my $pidfile;
  DB<2> n
main::(/home/k.sewnundun/test.pl:9):
9:              my $fileconfiguration;
  DB<2> n
main::(/home/k.sewnundun/test.pl:10):
10:             my $bakendhost;
  DB<2> n
main::(/home/k.sewnundun/test.pl:11):
11:             my $bakendport;
  DB<2> n
main::(/home/k.sewnundun/test.pl:12):
12:             my $sharememory;
  DB<2> n
main::(/home/k.sewnundun/test.pl:13):
13:             my $user;
  DB<2> n
main::(/home/k.sewnundun/test.pl:15):
15:             chomp $line;
  DB<2> n
main::(/home/k.sewnundun/test.pl:16):
16:             if ($line =~ m/-T\s+([0-9]+.*)+:(\d+)\s+/) {
  DB<2> n
main::(/home/k.sewnundun/test.pl:17):
17:                $ip = $1;
  DB<2> n
main::(/home/k.sewnundun/test.pl:18):
18:                $port = $2;
  DB<2> n
main::(/home/k.sewnundun/test.pl:5):
5:      my $ip;
  DB<2> print $line
3078 /opt/rh/varnish4/root/usr/sbin/varnishd -P /var/run-daemon/varnishd-toto4.pid -a 0.0.0.0:8080 -f /etc/varnish/etc/toto4/toto4-current.vcl -u varnish4-toto4 -g varnish4-toto4 -l 80M -T 0.0.0.0:38080 -p thread_pool_min=50 -p thread_pool_max=1000 -p thread_pool_timeout=120 -p vcl_dir=/etc/varnish/etc/toto4 -n /home/varnish/toto4/working -s malloc, -S /etc/varnish/etc/toto4/toto4.secret

  DB<3> n
main::(/home/k.sewnundun/test.pl:6):
6:              my $port;
  DB<3> n
main::(/home/k.sewnundun/test.pl:7):
7:              my $pid;
  DB<3> n
main::(/home/k.sewnundun/test.pl:8):
8:              my $pidfile;
  DB<3> n
main::(/home/k.sewnundun/test.pl:9):
9:              my $fileconfiguration;
  DB<3> n
main::(/home/k.sewnundun/test.pl:10):
10:             my $bakendhost;
  DB<3> n
main::(/home/k.sewnundun/test.pl:11):
11:             my $bakendport;
  DB<3> n
main::(/home/k.sewnundun/test.pl:12):
12:             my $sharememory;
  DB<3> n
main::(/home/k.sewnundun/test.pl:13):
13:             my $user;
  DB<3> n
main::(/home/k.sewnundun/test.pl:15):
15:             chomp $line;
  DB<3> n
main::(/home/k.sewnundun/test.pl:16):
16:             if ($line =~ m/-T\s+([0-9]+.*)+:(\d+)\s+/) {
  DB<3> n
main::(/home/k.sewnundun/test.pl:17):
17:                $ip = $1;
  DB<3> n
main::(/home/k.sewnundun/test.pl:18):
18:                $port = $2;
  DB<3> n
main::(/home/k.sewnundun/test.pl:5):
5:      my $ip;
  DB<3> print $line
3200 /opt/rh/varnish4/root/usr/bin/varnishncsa -a -n toto4 -w /home/logs/varnish/toto4/access.log -D -P /var/run-daemon/varnishncsa-toto4.pid -F %{X-Forwarded-For}i %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-agent}i"

  DB<4> n
main::(/home/k.sewnundun/test.pl:6):
6:              my $port;
  DB<4> n
main::(/home/k.sewnundun/test.pl:7):
7:              my $pid;
  DB<4> n
main::(/home/k.sewnundun/test.pl:8):
8:              my $pidfile;
  DB<4> n
main::(/home/k.sewnundun/test.pl:9):
9:              my $fileconfiguration;
  DB<4> n
main::(/home/k.sewnundun/test.pl:10):
10:             my $bakendhost;
  DB<4> n
main::(/home/k.sewnundun/test.pl:11):
11:             my $bakendport;
  DB<4> n
main::(/home/k.sewnundun/test.pl:12):
12:             my $sharememory;
  DB<4> n
main::(/home/k.sewnundun/test.pl:13):
13:             my $user;
  DB<4> n
main::(/home/k.sewnundun/test.pl:15):
15:             chomp $line;
  DB<4> n
main::(/home/k.sewnundun/test.pl:16):
16:             if ($line =~ m/-T\s+([0-9]+.*)+:(\d+)\s+/) {
  DB<4> n
main::(/home/k.sewnundun/test.pl:5):
5:      my $ip;
  DB<4> print $line
9803 varnishd -P /var/run-daemon/varnishd-varnish.pid -a 0.0.0.0:80 -f /etc/varnish/varnish.com/varnish.vcl -u varnish-varnish -l 80M -T 127.0.0.1:33080 -w 200,500,120 -n /home/varnish/varnish/working -s malloc,1G -p user varnish-varnish -p acceptor_sleep_decay 0.9 -p acceptor_sleep_incr 0.001 -p acceptor_sleep_max 0.05 -p auto_restart on -p ban_lurker_sleep 0.0 -p between_bytes_timeout 60.0 -p cli_buffer 8192 -p cli_timeout 5 -p clock_skew 10 -p connect_timeout 0.4 -p critbit_cooloff 180.0 -p default_grace 10 -p default_ttl 120 -p diag_bitmap 0x0 -p esi_syntax 0 -p expiry_sleep 1.0 -p fetch_chunksize 128 -p first_byte_timeout 60.0 -p group varnish-varnish -p http_range_support off -p listen_depth 1024 -p log_hashstring off -p log_local_address off -p lru_interval 2 -p max_esi_depth 5 -p max_restarts 4 -p ping_interval 3 -p pipe_timeout 60 -p prefer_ipv6 off -p queue_max 100 -p rush_exponent 3 -p saintmode_threshold 10 -p send_timeout 600 -p sess_timeout 5 -p sess_workspace 16384 -p session_linger 50 -p session_max 10000 -p shm_reclen 255 -p shm_workspace 8192 -p syslog_cli_traffic on -p vcl_trace off -p vcl_dir  -p waiter default

  DB<5> n
main::(/home/k.sewnundun/test.pl:6):
6:              my $port;
  DB<5> n
main::(/home/k.sewnundun/test.pl:7):
7:              my $pid;
  DB<5> n
main::(/home/k.sewnundun/test.pl:8):
8:              my $pidfile;
  DB<5> n

main::(/home/k.sewnundun/test.pl:9):
9:              my $fileconfiguration;
  DB<5> main::(/home/k.sewnundun/test.pl:10):
10:             my $bakendhost;
  DB<5> n
main::(/home/k.sewnundun/test.pl:11):
11:             my $bakendport;
  DB<5> n
main::(/home/k.sewnundun/test.pl:12):
12:             my $sharememory;
  DB<5> n
main::(/home/k.sewnundun/test.pl:13):
13:             my $user;
  DB<5> n
main::(/home/k.sewnundun/test.pl:15):
15:             chomp $line;
  DB<5> n
main::(/home/k.sewnundun/test.pl:16):
16:             if ($line =~ m/-T\s+([0-9]+.*)+:(\d+)\s+/) {
  DB<5> n
Complex regular subexpression recursion limit (32766) exceeded at /home/k.sewnundun/test.pl line 16.
 at /home/k.sewnundun/test.pl line 16
main::(/home/k.sewnundun/test.pl:17):
17:                $ip = $1;
  DB<5> q

可能是什么问题?感谢

1 个答案:

答案 0 :(得分:3)

这是你的正则表达式错误的一个好兆头。正如评论中所指出的那样 - 你是嵌套量词,这会导致问题,特别是当匹配重叠时......(例如.也匹配[0-9])。有几个人在评论中提到了这一点 - 特别是像([0-9]+.*)+这样的事情 - 如果找不到匹配,你最终不得不回溯各种不同的字母/数字组合来判断是否存在匹配。

如果您尝试use re 'debug'之类的内容,可以看到这一点,并注意您的正则表达式如何开始运行一系列疯狂的步骤来处理。

但是当你停止解析pgrep的输出时,这是一个没有实际意义的点,而是改为使用类似Proc::ProcessTable的东西:

#!/usr/bin/env perl
use strict;
use warnings; 
use Data::Dumper; 
use Proc::ProcessTable; 

my $ps = Proc::ProcessTable -> new; 

foreach my $proc ( grep { $_ -> {cmndline} =~ m/varnish/ } @{$ps->table} ) {
    my %opts = $proc -> {cmndline} =~ m/\-(\w) (\S+)/g;
    print Dumper $proc; 
    print Dumper \%opts;

    my ( $IP, $port ) = split ( ':', $opts{'T'});
}