@@ -2763,23 +2763,30 @@ Perl_sv_2nv_flags(pTHX_ SV *const sv, const I32 flags)
27632763}
27642764
27652765/*
2766- =for apidoc sv_2num
2766+ =for apidoc sv_2num_flags
2767+ =for apidoc_item sv_2num
2768+ X<SV_SKIP_OVERLOAD>
27672769
27682770Return an SV with the numeric value of the source SV, doing any necessary
27692771reference or overload conversion. The caller is expected to have handled
27702772get-magic already.
27712773
2774+ For sv_2num_flags() you can set C<SV_SKIP_OVERLOAD> in flags to avoid
2775+ any numeric context overloading.
2776+
27722777=cut
27732778*/
27742779
27752780SV *
2776- Perl_sv_2num (pTHX_ SV *const sv)
2781+ Perl_sv_2num_flags (pTHX_ SV *const sv, int flags )
27772782{
2778- PERL_ARGS_ASSERT_SV_2NUM;
2783+ PERL_ARGS_ASSERT_SV_2NUM_FLAGS;
2784+
2785+ assert((flags & ~SV_SKIP_OVERLOAD) == 0);
27792786
27802787 if (!SvROK(sv))
27812788 return sv;
2782- if (SvAMAGIC(sv)) {
2789+ if (SvAMAGIC(sv) && !(flags & SV_SKIP_OVERLOAD) ) {
27832790 SV * const tmpsv = AMG_CALLunary(sv, numer_amg);
27842791 TAINT_IF(tmpsv && SvTAINTED(tmpsv));
27852792 if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
@@ -8727,16 +8734,20 @@ S_sv_numcmp_common(pTHX_ SV **sv1, SV **sv2, const U32 flags,
87278734 if(!*sv2)
87288735 *sv2 = &PL_sv_undef;
87298736
8730- if(!(flags & SV_SKIP_OVERLOAD) &&
8731- (SvAMAGIC(*sv1) || SvAMAGIC(*sv2))) {
8732- if ((*result = amagic_call(*sv1, *sv2, method, AMGf_force_scalar)))
8733- return true;
8737+ if (SvAMAGIC(*sv1) || SvAMAGIC(*sv2)) {
8738+ if (!(flags & SV_SKIP_OVERLOAD)) {
8739+ if ((*result = amagic_call(*sv1, *sv2, method, AMGf_force_scalar)))
8740+ return true;
8741+ }
87348742
8735- /* normally handled by try_amagic_bin */
8743+ /* Normally handled by try_amagic_bin
8744+ This will do the normal RV to UV conversion
8745+ with SV_SKIP_OVERLOAD.
8746+ */
87368747 if (SvROK(*sv1))
8737- *sv1 = sv_2num (*sv1);
8748+ *sv1 = sv_2num_flags (*sv1, flags & SV_SKIP_OVERLOAD );
87388749 if (SvROK(*sv2))
8739- *sv2 = sv_2num (*sv2);
8750+ *sv2 = sv_2num_flags (*sv2, flags & SV_SKIP_OVERLOAD );
87408751 }
87418752
87428753 return false;
@@ -8826,7 +8837,6 @@ otherwise 'get' magic is ignored.
88268837=item C<SV_SKIP_OVERLOAD>
88278838
88288839Skip any operator overloading implemented for this type and operator.
8829- Be aware that numeric, C<+0>, overloading will still be applied, unless in the scope of C<no overloading;>.
88308840
88318841=back
88328842
0 commit comments