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

Re: Pre-RFC: yield true feature

Thread Previous | Thread Next
Neil Bowers
June 9, 2022 09:27
Re: Pre-RFC: yield true feature
Message ID:
Hi Brett,

> So maybe follow that kind of pattern. But I am still lost why
> typing "use feature 'yield_true';" is better than "1;".

As noted in the pre-RFC, and in Curtis’s draft RFC, the end-game is not to have people type

    use feature 'yield_true';

But for the feature to be included in the version bundle, so at the top of your module you just write:

    package Example;
    use v5.38;

And then you don’t have to put the 1; at the end.

> Looking back at the pre-RFC itself, I don't think the "motivation"
> is sufficiently, developed.

As noted in the Pre-RFC, the requirement to add "1;" confuses people. And they might look at the code for CPAN modules and see humorous return values, and be further confused.

Here’s an example. Someone writes the following module:

    package Example;
    use v5.36;
    use parent 'Exporter';
    our @EXPORT_OK = qw/ hello_world /;
    sub hello_world { print "hello, world\n"; }

They use it from a script, and it works fine.
They’re expanding it, and they start off by adding the following line after the EXPORT_OK line:

    my $secret = 0;

Now when they run their test script, they get an error: did not return a true value at line 6.

Huh? They look at their module, and line 6 is where they declared the sub, which was fine before, and they’ve not touched it. It just happens that the last value returned by loading that module is now 0.

If they know to look at perldiag, they’d find an explanation, or if they google "perl did not return a true value" they’d also find the answer.

Instead of returning 0 to signify an initialisation failure, I think it’s better to throw an exception (die), and not require this quirky true value at the end of your modules.

Yes, it’s only a small thing, but the cumulative effect of addressing small quirks like this is a more welcoming / less confusing language for beginners and casual programmers. We’d never expect people to use this feature explicitly, it just becomes part of "latest and greatest Perl".


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About