Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^4: Bug in Class::Struct?

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


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

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

Replies are listed 'Best First'.
Re^5: Bug in Class::Struct?
by haukex (Archbishop) on Apr 23, 2020 at 20:59 UTC

    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

      That’s very interesting…

      perl -MYAML -E '$vol = "asdf"; $x = { moo => scalar($vol =~ m/^HOLDING +/) }; say $x->{moo} == 0' 1

      But also, to me at least, very surprising and I wouldn’t want to see it in code.

        I guess it depends: mapping it to a different value, as in bool ? 1 : 0, will definitely make it more clear (and also force scalar context). But using scalar is better than not using it at all, since if you've got a hash { moo => $vol =~ m/^HOLDING/, foo => 'bar' } that's asking for trouble (and in some cases, security holes, like with CGI.pm's param).

        Update: Corrected the example hash

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found