in reply to Re: Extracting /regex/mg in a while loop
in thread Extracting /regex/mg in a while loop

Nice work!

About that CAVEAT for Windows: File::Spec covers that in a portable way: $devnull = File::Spec->devnull();

  • Comment on Re^2: Extracting /regex/mg in a while loop

Replies are listed 'Best First'.
Re^3: Extracting /regex/mg in a while loop
by kcott (Archbishop) on Oct 10, 2023 at 16:20 UTC

    G'day haj,

    ++ Thanks for the tip.

    For anyone looking for portability, DRYness, and an easy way to add more tests, replace all the code prior to the start of the subroutine definitions with:

    Update: I made an error with the original code I posted. I've stricken that code and replaced it with a rewrite which works correctly. That rewrite includes all of the additional features: "portability, DRYness, and an easy way to add more tests". The code I posted below is no longer valid; I've stricken it and placed it in a spoiler. The final paragraph, about adding new tests, is still valid: I left it unchanged.

    #!/usr/bin/env perl use v5.38; use autodie; use constant { SEP1 => "\n" . '=' x 40, SEP2 => '-' x 40, BENCH_MULT => 1_000, }; use Benchmark 'cmpthese'; use File::Spec (); my ($null_fh, $base_str); BEGIN { open $null_fh, '>', File::Spec::->devnull(); $base_str = <<~'EOT'; AdataA BdataB CdataC EOT } my @bench_names = qw{net1 net2 hau1 ner1 ner2 kco1 kco2}; my (%bench_for, %benches_to_cmp); say 'Test', SEP1; for my $bench (@bench_names) { no strict 'refs'; ($bench_for{$bench} = \&{$bench})->(); use strict 'refs'; say SEP2; $benches_to_cmp{$bench} = sub { $bench_for{$bench}->(BENCH_MULT, $null_fh) }; } say 'Bench', SEP1; cmpthese 0 => \%benches_to_cmp; say SEP2;

    I've run this a few times: the results are comparable with what I posted earlier.

    The multiplier for $base_str is now the constant BENCH_MULT; play around with this if you're so inclined.

    For more tests, add your subroutine and its name to @bench_names; the rest is done for you.

    — Ken