aboutsummaryrefslogtreecommitdiff
path: root/challenge-280/dave-jacoby/perl/ch-2.pl
blob: e4a6a683a6f8238fe27d5d66576470f427c67bb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env perl

use strict;
use warnings;
use experimental qw{ fc say postderef signatures state };

my @examples = (

    'p|*e*rl|w**e|*ekly|',
    'perl',
    'th|ewe|e**|k|l***ych|alleng|e',

);

for my $input (@examples) {
    my $output = count_asterisks($input);

    say <<"END";
    Input:  \$str = "$input"
    Output: $output
END
}

sub count_asterisks ($str) {
    $str =~ s{
                # if we can comment a regex, we probably should
        \|      # a pipe character
        [^\|]*  # zero or more non-pip characters
        \|      # a pipe character
        }{  }gmix;

    # the = () = forces it into a list context, and otherwise
    # you'd get a boolean result.
    my $c = () = $str =~ /(\*)/gmix;
    return $c;
}