scalar context won’t materialize a zero though. Does change the undef to an empty string
Actually, it kind of does: the match operation changes its return value based on context, so the special false value returned in scalar context is zero in numeric context. (So it's also not changing the undef to an empty string, it's changing the behavior of the match.)
| [reply] [Watch: Dir/Any] [d/l] [select] |
perl -MYAML -E '$vol = "asdf"; $x = { moo => $vol =~ m/^HOLDING/ }; sa
+y Dump $x'
---
moo: ~
perl -MYAML -E '$vol = "asdf"; $x = { moo => scalar($vol =~ m/^HOLDING
+/) }; say Dump $x'
---
moo: ''
| [reply] [Watch: Dir/Any] [d/l] |
Yep, your example shows what I'm talking about :-) Remember Perl's special false value will look like both the empty string and the number zero at the same time (it's basically a dualvar), in your YAML it's showing up as its string version. To identify Perl's special false value, you actually have to look a tiny bit into the internals. The first example below shows that in list context, a failed match is returning the empty list (which in a hash translates to undef; add -w to your first example and you get Odd number of elements in anonymous hash). The second example shows that forcing scalar context on the match causes it to return Perl's special false value.
$ perl -MDevel::Peek -le 'my @x = /abc/; Dump(@x)'
SV = PVAV(0x51f85b346e78) at 0x51f85b3c66b8
REFCNT = 1
FLAGS = ()
ARRAY = 0x0
FILL = -1
MAX = -1
FLAGS = (REAL)
$ perl -MDevel::Peek -le 'my @x = scalar(/abc/); Dump(@x)'
SV = PVAV(0x579bb31a2e38) at 0x579bb31c86a8
REFCNT = 1
FLAGS = ()
ARRAY = 0x563bc3d01720
FILL = 0
MAX = 3
FLAGS = (REAL)
Elt No. 0
SV = PVNV(0x579bb31a1f40) at 0x579bb31a15a8
REFCNT = 1
FLAGS = (IOK,NOK,POK,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x579bb31cd6e4 ""\0
CUR = 0
LEN = 10
| [reply] [Watch: Dir/Any] [d/l] [select] |