develooper Front page | perl.perl5.porters | Postings from July 2018

Re: Allowing require to return false

Thread Previous | Thread Next
From:
John Imrie via perl5-porters
Date:
July 28, 2018 11:00
Subject:
Re: Allowing require to return false
Message ID:
15b59da7-e28f-af4f-4d85-e1ea177cf4d1@virginmedia.com


On 27/07/2018 01:02, James E Keenan wrote:
> On 07/26/2018 12:43 PM, Dave Mitchell wrote:
>> On Wed, Jul 25, 2018 at 09:56:12AM -0400, David Farrell wrote:
>>> Frustratingly, I don't know if any of these are worth pursuing - I 
>>> wonder
>>> if anyone would write modules that didn't return a true value as they
>>> wouldn't work on past versions of Perl. If we assume Perl is going 
>>> to be
>>> used for another 25 years, then maybe it's worth it.
>>
>> I'm opposed to making any change here.
>>
>> It's an annoying but trivial little quirk that people have to remember.
>> If we change it (such as adding adding a feature, or use v5.30) then for
>> the next 20 years people have to remember *2* quirks instead.
>>
>> Just removing it altogether might not break *many* modules, but why 
>> break
>> *anything* for so little gain?
>>
>>
> +1
>
> 1;
There may be a way of implementing this with out too much trouble.
1st make it controlled by a feature inside the module, which means that 
it wont break old code
2nd If require is called in void context assume that the last line of 
the module was '0 but true'; iff the module returns a false value;
3rd Anything else works as before

This allows you to write modules like
package Module;
use v5.30;
# my stuff
__END__

And the following in your main code
use v5.30;
require Module; # Assumes that Module returns '0 but true'
if (require Module) {
     # Called in boolean context so '0 but true' is not added to the end 
of the module
}

However if Module is written as
package Module;
use v5.30;
# my stuff
3;
__END__

The require will return 3 in all contexts

John

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