#!/usr/bin/perl use strict; use warnings; use YAML::Any qw(DumpFile LoadFile); my $yaml_file = 'test-re.yaml'; my @default_patterns = ( qr/^foo.*bar.*baz/i, qr/^bar.*baz.*quux/i, qr/^quux$/, ); my $yaml = {}; if ( -w $yaml_file ) { $yaml = LoadFile( $yaml_file ); } if ( scalar @ARGV ) { push @{$yaml->{patterns}}, qr/$_/ for @ARGV; } else { # If no args, assume we want a fresh pattern dump, seed # with the default list of patterns. # $yaml->{patterns} = []; push @{$yaml->{patterns}}, $_ for @default_patterns; } DumpFile( $yaml_file, $yaml ) or die "Error writing yaml file: $!\n"; print "Wrote $yaml_file\n"; #### $ ls test-re.yaml ls: cannot access test-re.yaml: No such file or directory $ test-yaml-re.pl Wrote test-re.yaml $ cat test-re.yaml --- patterns: - !!perl/regexp (?^i:^foo.*bar.*baz) - !!perl/regexp (?^i:^bar.*baz.*quux) - !!perl/regexp (?^:^quux$) $ test-yaml-re.pl '^foo$' Wrote test-re.yaml $ cat test-re.yaml --- patterns: - !!perl/regexp (?^:(?^i:^foo.*bar.*baz)) - !!perl/regexp (?^:(?^i:^bar.*baz.*quux)) - !!perl/regexp (?^:(?^:^quux$)) - !!perl/regexp (?^:^foo$) $ test-yaml-re.pl '^bar$' Wrote test-re.yaml $ cat test-re.yaml --- patterns: - !!perl/regexp (?^:(?^:(?^i:^foo.*bar.*baz))) - !!perl/regexp (?^:(?^:(?^i:^bar.*baz.*quux))) - !!perl/regexp (?^:(?^:(?^:^quux$))) - !!perl/regexp (?^:(?^:^foo$)) - !!perl/regexp (?^:^bar$) $ test-yaml-re.pl '^baz$' Wrote test-re.yaml $ cat test-re.yaml --- patterns: - !!perl/regexp (?^:(?^:(?^:(?^i:^foo.*bar.*baz)))) - !!perl/regexp (?^:(?^:(?^:(?^i:^bar.*baz.*quux)))) - !!perl/regexp (?^:(?^:(?^:(?^:^quux$)))) - !!perl/regexp (?^:(?^:(?^:^foo$))) - !!perl/regexp (?^:(?^:^bar$)) - !!perl/regexp (?^:^baz$)