Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Bug in Class::Struct?

by Your Mother (Archbishop)
on Apr 23, 2020 at 20:29 UTC ( [id://11115976]=note: print w/replies, xml ) Need Help??


in reply to Re: Bug in Class::Struct?
in thread Bug in Class::Struct?

scalar context won’t materialize a zero though. Does change the undef to an empty string; at least on perl 5.18.

Replies are listed 'Best First'.
Re^3: Bug in Class::Struct?
by haukex (Archbishop) on Apr 23, 2020 at 20:46 UTC
    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.)

      ¿Que?

      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: ''

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11115976]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-29 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found