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

Re: Allowing require to return false

Thread Previous | Thread Next
From:
Tomasz Konojacki
Date:
July 26, 2018 00:16
Subject:
Re: Allowing require to return false
Message ID:
20180726021043.F15E.5C4F47F8@xenu.pl
On Wed, 25 Jul 2018 09:56:12 -0400
David Farrell <davidnmfarrell@gmail.com> wrote:

> Hi P5P folks,
> 
> A feature of Perl I really don't like is requiring modules to return a true
> value. Here's why:
> 
>    - It feels gross appending a "1;" to every module I write
>    - All Perl can do is croak with a generic error message that isn't very
>    useful: "Foo.pm did not return a true value".
>    - If a module encounters an init error should die with a meaningful
>    error message instead
>    - I don't think any other language has copied this feature ....
>    including Perl 6
>    - I would guess the only time any of us see this message is when we've
>    forgotten to append "1;" to a new module
>    - Removing it would not affect the vast majority (all?) of modules
> 
> I'm a Perl source code newbie but I wrote about a couple of implementations
> here:
> 
> https://www.perl.com/article/patching-perl-loading-modules-that-return-false/
> 
> I can imagine a few ways to do it:
> 
>    1. Remove the feature altogether - this would be breaking change if any
>    modules use this feature
>    2. Make it a feature - this would be safer but I doubt anyone would use
>    the feature
>    3. Deprecate the error and remove it in a future version
> 
> 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.
> 
> What does everyone else think?
> 
> Thanks in advance,
> 
> David

Personally, I'd *love* to see this enhancement in perl. Having to put
'1;' at the end of module certainly puts off newcomers and does not
provide any real benefits.

However, I don't think returning false value when require succeeds is a
good idea. For modules without a positive return value, IMHO it should
be hardcoded to 1. It's a minor problem, but people sometimes write if
require(...). Of course, currently this would simply throw an exception,
so there is no breakage, but I think that, for consistency, require()
should always return a true value on success.

I'm sure someone will prove me wrong, but I believe implementing this
change would *not* introduce significant breakage, and thus, it
shouldn't require feature flag. I'm struggling to imagine how
real-life code relying on current require() behaviour would look like.

I'm almost certain that this change would cause *much* less trouble than
many of bugfixes introduced in recent major releases.

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