簡體   English   中英

重構鏈式elsif(帶正則表達式)在Perl中調度表。 但我的代碼如何工作?

[英]Refactored chained elsif (with regex) to dispatch table in Perl. But how my code works?

我有一個很長的鏈接,如果elsif像這樣阻止我使用Capture :: Tiny捕獲命令的輸出,然后使用一系列正則表達式提取它。

sub capture_cpu_test {
    my ($cmd) = @_;
    print '--------------------', "\n";
    print 'COMMAND is: ', "$cmd", "\n";

    #capture an output of sysbench command
    my ( $stdout_to_format, $stderr, $exit ) = capture {
        system($cmd );
    };

    #split on newline to separately analyze later
    my @output_lines = split( "\n", $stdout_to_format );

    #set hash to collect param => param_value pairs
    my %plotting_hash = ();

    foreach my $line (@output_lines) {
        if ( $line =~ m/\ANumber\s+of\s+threads:\s+(\d+)\z/xms ) {
            $plotting_hash{num_threads}   = $1;
        }

        #long list of elseif continues

        elsif ( $line =~ m{\A\s+events\s+\(avg\/stddev\):\s+(.+?)\/(.+?)\z}xms ) {
            $plotting_hash{events_avg}    = $1;
            $plotting_hash{events_stddev} = $2;
        }
    }

    #returns ref to plotting_hash to combine upstream
    my $hash_plot_ref = \%plotting_hash;
    print 'Printing $hash_plot_ref inside capture_cpu_test: ', "\n";
    print Dumper($hash_plot_ref);
    return $hash_plot_ref;
}

我想讓它更具可讀性,所以我把這個elsif塊更改為使用網上的答案調度表,沒問題,它的工作原理如下:

#set dispatch table with regex => sub {} pairs
my %plotting_hash  = ();
my %dispatch_regex = (
    qr/\ANumber\s+of\s+threads:\s+(\d+)\z/xms => 
      sub { $plotting_hash{num_threads}   = $1 },

    #more entries here

    qr{\A\s+events\s+\(avg\/stddev\):\s+(.+?)\/(.+?)\z}xms =>
      sub { $plotting_hash{events_avg}    = $1;
            $plotting_hash{events_stddev} = $2; },
);

#populate the %plotting_hash by calling dispatch table (%dispatch_regex)
my $code_ref;
foreach my $line (@output_lines) {
    foreach my $regex ( keys %dispatch_regex ) {
        if ( $line =~ $regex ) {
            $code_ref = $dispatch_regex{$regex};
            $code_ref->();
            last;
        }
    }
}

我得到這樣的東西:

$hash_plot_ref = {
      'num_threads'         => '4',
      'events_stddev'       => '50.98',
      'events_avg'          => '2500.0000',
      ...
};
  1. 我想知道從數據行上的正則表達式到匿名子程序的調度是如何工作的。 如何將捕獲($ 1,$ 2)轉移到anon sub? 這個anon subs究竟如何得到參數? 我試圖用B :: Deparse來解決它,但它並沒有說明多少。
  2. 我怎么能把它寫得更具可讀性? 我嘗試使用制表三元組和for / when(這里沒有顯示),但它看起來仍然不如鏈接elseif。
  1. 我想知道從數據行上的正則表達式到匿名子程序的調度是如何工作的。 如何將捕獲($ 1,$ 2)轉移到anon sub? 這個anon subs究竟如何得到參數? 我試圖用B :: Deparse來解決它,但它並沒有說明多少。

$ 1和$ 2就像全局變量一樣。 它們隨處可見。 執行以下代碼行時:

if ( $line =~ $regex ) {

如果成功,$ 1和$ 2等將獲得該成功匹配的值。 順便說一下,你知道了

(something)

正則表達式引擎使用位來提供1美元,2美元等?

  1. 我怎么能把它寫得更具可讀性? 我嘗試使用制表三元組和for / when(這里沒有顯示),但它看起來仍然不如鏈接elseif。

實際上,一旦你了解了正在發生的事情,它就不會太糟糕。 對我來說,我看到一個模式和一個相關的塊。 我喜歡。 各個部分更靠近在一起,比它們分布在更多的線條上更容易看到。 給自己一些時間來理解,很快就會開始看起來很有意義。

如果不花一些時間思考 ,我不確定你對三元的意思, - 你真的想要澄清嗎? 如果是這樣,請發布一個新問題,或向此添加更多信息,以便解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM