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

Re: Pre-RFC: yield true feature

Thread Previous | Thread Next
From:
Yuki Kimoto
Date:
June 24, 2022 01:13
Subject:
Re: Pre-RFC: yield true feature
Message ID:
CAExogxPzUJ1JvV3Th27QY1DKnNy3DUBK26Q_Py7oibm6J4XdOw@mail.gmail.com
2022-6-24 5:52 Graham Knop <haarg@haarg.org> wrote:

> On Wed, Jun 22, 2022 at 1:19 AM Yuki Kimoto <kimoto.yuki@gmail.com> wrote:
> >
> > Summary.
> >
> > Are we going in the following direction?
> >
> > If the "yield_true" feature is enabled,
> >
> >   - the effect is local-scope. Generally the "yield_true" feature is
> used at the top level file scope.
> >   - It has no effect on the return value of .pm file. Both an implicit
> return value and an explicit return value specified by return statement.
> >   - "require" ignores the return value to determine if the module was
> successfully loaded.
> >   - "require" returns the return value of .pm file just like the current
> behavior.
> >
>
> I think having require return the last value from the file would be a
> terrible change.
>
> Currently, require will always return a true value, because anything
> else will croak. There is plenty of code that relies on this. For
> example:
>
> if (eval { require $module }) { ... }
>
> This has always been a reliable way to check if the module could be
> loaded. If require continues to return the (possibly false) value from
> the file, plenty of code will break. In fact, using the return value
> of require as a true value has been the only reliable use of its
> return value, since it will not return the "last value" after the
> first call to require for a given file.
>
> I strongly believe that when require loads a file that is using this
> feature, it should ignore the final value from the module and instead
> return a standard boolean true.
>

Summary again.

 - the effect is local-scope. Generally the "yield_true" feature is used at
the top level file scope.
- It has no effect on the return value of .pm file. Both an implicit return
value and an explicit return value specified by return statement.
- "require" ignores the return value to determine if the module was
successfully loaded.
- "require" always returns true value except when the exceptions are thrown
differently from the current behavior. (Changed)

Current behavior of require:

  # Foo.pm
  package Foo;

  "ABC";

  # main.pl
  use strict;
  use warnings;
  use FindBin;
  use lib "$FindBin::Bin";

  my $ret = require Foo;

  # ABC
  warn $ret;

  my $ret2 = require Foo;

  # 1
  warn $ret2;

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