Consider the following program:
perl -MDevel::Peek -le 'my @nonempty = (1); my @empty = (); print Dump +(scalar @nonempty); print Dump(scalar @empty)' SV = IV(0x563dfe2d16f0) at 0x563dfe2d1700 REFCNT = 1 FLAGS = (TEMP,IOK,pIOK) IV = 1 SV = PVNV(0x563dfe2d0200) at 0x563dfe2ce410 REFCNT = 2147483647 FLAGS = (PADTMP,IOK,NOK,POK,READONLY,PROTECT,pIOK,pNOK,pPOK) IV = 0 NV = 0 PV = 0x7fb0f957d3c2 "0" CUR = 1 LEN = 0
Which is to say, scalar returns a PV if the array is empty, and an IV if it is not. This can be a problem, because most serializers, including JSON modules, will encode the PV as "0" and the IV as 1, which will cause a problem if the consumer of the resulting JSON expects strict type conformance.
Is there a logical reason for this discrepancy?
(Yes, I know that I could just do 0+scalar @array - my point is that that shouldn't be necessary.)
In reply to Reason for this discrepancy with scalar? by kikuchiyo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |