develooper Front page | perl.perl5.porters | Postings from January 2017

[perl #78288] ref and other ops are inefficient in boolean context

Thread Next
From:
James E Keenan via RT
Date:
January 1, 2017 00:09
Subject:
[perl #78288] ref and other ops are inefficient in boolean context
Message ID:
rt-4.0.24-17033-1483229370-747.78288-15-0@perl.org
On Fri, 08 Oct 2010 13:29:35 GMT, timbo wrote:
> 
> This is a bug report for perl from Tim.Bunce@pobox.com,
> generated with the help of perlbug 1.39 running under perl 5.12.2.
> 
> 
> -----------------------------------------------------------------
> [Please describe your issue here]
> 
> [16:05] timbunce: code like "if ref $foo" is far more expensive than
> it needs to be because ref() still creates a new SVPV when called in a
> boolean context.
> [16:08] xdg: timbunce, could you please open a ticket on that? I'm
> sure someone can optimize that.
> [16:10] • Nicholas wonders if "if ref $foo" can be spotted with much
> the same code as "if %hash" is
> [16:10] Zefram: yes it can, at op check time
> [16:11] timbunce: How can internals determine "boolean context"?
> [16:11] vincent: at op check time
> [16:11] timbunce: there are probably other ops that could do less work
> in boolean context
> [16:11] Nicholas: offhand, "I don't know", but I know that "if %hash"
> does this
> 
> [16:11] Zefram: but ref($foo)'s truth value isn't as simple as most
> people expect, so it's approximately never what a programmer actually
> wants
> [16:12] timbunce: Zefram: true, but that doesn't stop people writing
> "if ref $foo" often
> [16:13] Zefram: I'd be more inclined to use check-time logic to emit a
> warning for it
> [16:14] Zefram: "Dubious use of reference-type operator in truth-value
> context"
> [16:14] timbunce: Zefram: I think you'd have angry hordes after you.
> [16:14] timbunce: better done in perlcritic
> [16:14] PerlJam: timbunce: +1
> [16:14] mst: Zefram: but then Scalar::Defer won't work anymore
> [16:14] Zefram: Scalar::Defer is an abomination
>  [16:15] mst: Zefram: an abomination that's heavily used in the
> pumpking's production code
> [16:16] timbunce: mst Zefram: I think Scalar::Defer could still work.
> The pp_ref code just needs to know if it should return sv_yes or sv_no
> 
> In boolean context, pp_ref would check the pv returned by sv_reftype
> in
> the same way that SvTRUE does (for "" and "0") and the push &sv_yes or
> &sv_no.
> 

Tim Bunce proposed this approach more than six years ago.

Can anyone respond?

Thank you very much.

-- 
James E Keenan (jkeenan@cpan.org)

---
via perlbug:  queue: perl5 status: new
https://rt.perl.org/Ticket/Display.html?id=78288

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About