When using the given/when construct in Perl, which is an experimental feature, there are several common pitfalls you should be aware of. These can affect how your code behaves, especially given the use of smart matching (~~). Below are some of the notable gotchas:
Below is a code example demonstrating some of these pitfalls:
#!/usr/bin/perl
use v5.10;
use strict;
use warnings;
my $value = '10';
given ($value) {
when (10) { say "It's the number 10!" } # This will match due to smart matching.
when ('10') { say "It's a string '10'!" } # This can lead to confusion.
when (undef) { say "It's undefined!" } # Careful with undefined values.
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?