x +{ %hash{grep !/^schema|log$/, keys %hash } }
$exclude = sub { my $href = shift; my $excl= join '|', @_; return +{ %$href{ grep !/^$excl$/, keys %$href } } }
Noting, of course, that because there are no parentheses in either of these regex, the ^ anchor only applies to the first term in the alternation list, the $ anchor only applies to the final term in the alternation list, and any intermediate terms will be completely unanchored, which means that it's likely to exclude more keys than you might think: therefore, running ->exclude(qw/log schema/) will not necessarily match running ->exclude(qw/schema log/) , depending on the keys of the hash, which may or may not have been the intention.
DB<1> @hash{'schema','log',a..c, 'schematic', 'blog'}=('s','l',1..3,
+'circuit','rant')
DB<2> x \%hash
0 HASH(0x3da37a8)
'a' => 1
'b' => 2
'blog' => 'rant'
'c' => 3
'log' => 'l'
'schema' => 's'
'schematic' => 'circuit'
DB<3> x +{ %hash{grep !/^schema|log$/, keys %hash } } # Note that t
+he ^ only applies to schema, and the $ only applies to log, so it wil
+l remove more than you might have meant
0 HASH(0x3f6e6f8)
'a' => 1
'b' => 2
'c' => 3
DB<4> x +{ %hash{grep !/^(schema|log)$/, keys %hash } } # This vers
+ion has all exclusions fully anchored
0 HASH(0x3f6ed10)
'a' => 1
'b' => 2
'blog' => 'rant'
'c' => 3
'schematic' => 'circuit'
DB<5> $exclude1 = sub { my $href = shift; my $excl= join '|', @_;
+return +{ %$href{ grep !/^$excl$/, keys %$href } } }
DB<6> $h_ref = \%hash
DB<7> x $h_ref->$exclude1(qw/schema log/)
0 HASH(0x3f73640)
'a' => 1
'b' => 2
'c' => 3
DB<8> x $h_ref->$exclude1(qw/log schema/)
0 HASH(0x3f73988)
'a' => 1
'b' => 2
'blog' => 'rant'
'c' => 3
'schematic' => 'circuit'
DB<9> $exclude2 = sub { my $href = shift; my $excl = join '|', @_; r
+eturn +{ %$href{ grep !/^($excl)$/, keys %$href } } }
DB<10> x $h_ref->$exclude2(qw/schema log/)
0 HASH(0x3f73808)
'a' => 1
'b' => 2
'blog' => 'rant'
'c' => 3
'schematic' => 'circuit'
| [reply] [d/l] [select] |
Well spotted by you and badly tested by me, thanks.
In the end the regex is a poor man's alternative for the unfortunately designed smartmatch operator ~~
Actually I was too occupied worrying if I need to quotemeta the input or if keeping the door open for regex-metas is a better interface, and forgot about precedence here.
So another "fault" in the concept. =)
| [reply] |