In the following code, I get an uninitialized value warning, but only in the second given/when example. Why is this?
#!/usr/bin/env perl
use warnings;
use 5.12.0;
my $aw;
given ( $aw ) {
when ( 'string' ) {
say "string";
}
when ( not defined ) {
say "aw not defined";
}
default {
say "something wrong";
}
}
given ( $aw ) {
when ( /^\w+$/ ) {
say "word: $aw";
}
when ( not defined ) {
say "aw not defined";
}
default {
say "something wrong";
}
}
The output I get is:
aw not defined
Use of uninitialized value $_ in pattern match (m//) at ./perl.pl line 20.
aw not defined
given/whenuses the “smartmatch operator“:~~.undef ~~ stringis:Thus there is no warning here.
undef ~~ regexis:And a warning is produced when trying to match on
undef.