STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) {
SV *svr;
- I32 len;
+ I32 len, has_array;
U32 ret;
dSP;
int count;
+ has_array = SvTYPE(sv) == SVt_PVAV;
+
ENTER;
SAVETMPS;
EXTEND(SP, 3);
PUSHs(sv_2mortal(newRV_inc(sv)));
PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
- if (SvTYPE(sv) == SVt_PVAV) {
+ if (has_array) {
len = av_len((AV *) sv) + 1;
mPUSHi(len);
} else {
- len = 1;
+ len = 0;
PUSHs(&PL_sv_undef);
}
PUTBACK;
FREETMPS;
LEAVE;
- return ret - 1;
+ return has_array ? ret - 1 : ret;
}
STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) {
use strict;
use warnings;
-use Test::More tests => 11;
+use Test::More tests => 13;
use Variable::Magic qw/wizard cast VMG_COMPAT_SCALAR_LENGTH_NOLEN/;
my $c = 0;
-my $n = int rand 1000;
+my $n = 1 + int rand 1000;
my $wiz = wizard len => sub { ++$c; return $n };
is($c, 0, 'len : create wizard');
is($c, 3, 'len : get array length 0');
is($b, 0, 'len : get array length 0 correctly');
-$c = 0;
-$n = int rand 1000;
-# length magic on scalars needs also get magic to be triggered.
-$wiz = wizard get => sub { return 56478 },
- len => sub { ++$c; return $n };
+SKIP: {
+ skip 'length() no longer calls mg_len magic' => 5 if VMG_COMPAT_SCALAR_LENGTH_NOLEN;
-my $x = int rand 1000;
+ $c = 0;
+ $n = 1 + int rand 1000;
+ # length magic on scalars needs also get magic to be triggered.
+ $wiz = wizard get => sub { return 56478 },
+ len => sub { ++$c; return $n };
-SKIP: {
- skip 'length() no longer calls mg_len magic', 3 if VMG_COMPAT_SCALAR_LENGTH_NOLEN;
+ my $x = int rand 1000;
cast $x, $wiz;
is($c, 0, 'len : cast on scalar');
$b = length $x;
- is($c, 1, 'len : get scalar length');
- is($b, $n - 1, 'len : get scalar length correctly');
+ is($c, 1, 'len : get scalar length');
+ is($b, $n, 'len : get scalar length correctly');
+
+ $n = 0;
+ $b = length $x;
+ is($c, 2, 'len : get scalar length 0');
+ is($b, $n, 'len : get scalar length 0 correctly');
}