develooper Front page | perl.perl5.porters | Postings from June 2022

Re: Pre-RFC: yield true feature

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
June 28, 2022 09:29
Subject:
Re: Pre-RFC: yield true feature
Message ID:
YrrJ6XjdC2Dm0iov@iabyn.com
On Mon, Jun 27, 2022 at 01:12:17PM +0200, Alexander Hartmaier wrote:
> package B:
> my $rv = require 'A';
> # $rv is 0 which is false in boolean context
> 
> # no error in scope of package B although false is returned


My mind has been changed by Graham Knop's observations that:

* the return value of require is most often used like:

    if (eval { require $module }) { ... }

* that if a file is require'd for a second time, the code isn't called
again and the 'true' value (i.e. !!1) is returned rather than whatever
was returned the first time. For example:

    Foo.pm:
        package Foo;
        5;

    $ perl -le'print require Foo for 1..2'
    5
    1

So relying on the actual return value is a poor practice which we should
seek to eliminate. So I now think that the single change in behaviour in
perl should be:

    At the point in the core which does the return-from-require
    processing, it's behaviour should change from:

        if the return value is false, croak, else return the value to the
        caller of 'require'.

    to:

        if 'require_returns_true' was in scope at the point where require
        explicitly or implicitly returned:

            throw away the return value and return !!1 to the caller of
            'require'

        else

            as before: if the return value is false, croak, else return
            the value to the caller of 'require'.


I'm not convinced that having a warning when returning a false value would
be useful. I suspect that most uses of 'use v5.40' will be in new code
(rather than retrofitting an existing module) where a warning won't be
particularly useful. Instead it could generate false positives - for
example, if the last line in Foo.pm file is:

    $Foo::some_flag = calc_flag();

then the warning will surface only if calc_flag() returns false,
which might not happen during testing.

So such a warning would only potentially be useful in old code which has
been retrofitted with 'use v5.40', and then only on those rare modules
which actually use a false return to signal failure. I think this is rare
enough to not outweigh the problem of false positives in new code.


-- 
31 Dec 1661: "I have newly taken a solemne oath about abstaining from plays".
 1 Jan 1662: "And after ... we went by coach to the play".
    -- The Diary of Samuel Pepys

Thread Previous | 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