in reply to Re^4: Try::Tiny and -E
in thread Try::Tiny and -E

you are wrong on both accounts.

> It doesn't matter when the Try::Tiny is compiled.

I've explicitly tested this with feature::feature_enabled() and order definitely matters.

It's basically the difference between:

use feature "try"; use Try::Tiny;

vs

use Try::Tiny; # no chance (well see footnote°) use feature "try";

And that's because lexical scope starts right at the point of declaration.

> it's out of reach of Try::Tiny since I believe it's intentionally a pure-Perl module.

It's very much possible with a pure-Perl module like I said. The _enabled routines in feature are basically checking the hinthash %^H returned by caller for the needed level.

And the hint-key is called "feature_try"

A slight complication is that feature introduced "try" before it provided the *_enabled checks. IOW one needs to copy the hint-hash lookup logic for older Perl versions.

Footnotes

°) the wrong order could be fixed by using an INIT {} block doing the check after all compilations are completed, but I question the wisdom of such a fragile construct. Too many edge cases.¹

¹) Turns out that neither INIT,CHECK or UNITCHECK can see the hint anymore.

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

update

little demo v5.38

perl -E'BEGIN{say join "\n",feature::features_enabled()}' bareword_filehandles bitwise current_sub evalbytes fc indirect isa module_true multidimensional postderef_qq say signatures state unicode_eval unicode_strings

Replies are listed 'Best First'.
Re^6: Try::Tiny and -E
by ikegami (Patriarch) on Dec 29, 2025 at 07:28 UTC

    It's very much possible with a pure-Perl module like I said. The _enabled routines in feature are basically checking the hinthash %^H returned by caller for the needed level.

    But the problem is that the code isn't even compiled into a call to Try::Tiny's try, so how is it supposed to check the hints? That requires hooking into the compiler.

      Not sure what you are objecting about, the newest feature has code examples showing how an import() of a module can check the hinthash of the caller.

      Try::Tiny or others could do this too, if they come second.

      From feature_enabled($feature, $depth)

      package MyStandardEnforcer; use feature (); use Carp "croak"; sub import { croak "disable indirect!" if feature::feature_enabled("indirect"); }

      And yes, the implementation of feature_enabled() is in pure Perl.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        That solution will give both false positives (warn when it shouldn't) and false negatives (not warn when it should). The order loaded will determine if you can get false positives or false negatives, but it you can get false results regardless of the order in which they are loaded. Again, the order in which you load the modules is irrelevant. And that's because you're checking at the wrong time to get consistent results. For that, you need to check when try use is compiled, not when the module is loaded.

Re^6: Try::Tiny and -E
by ikegami (Patriarch) on Dec 29, 2025 at 07:01 UTC

    Any reason you didn't provide your tests? Because my testing confirms what I said.

    $ perl -e' use feature qw( try ); use Try::Tiny; try { }; CORE::say "ok"; ' syntax error at -e line 4, near "};" Execution of -e aborted due to compilation errors.

    vs

    $ perl -e' use Try::Tiny; use feature qw( try ); try { }; CORE::say "ok"; ' syntax error at -e line 4, near "};" Execution of -e aborted due to compilation errors.

    And

    $ perl -e' use feature qw( try ); use Try::Tiny; { no feature qw( try ); try { }; } CORE::say "ok"; ' ok

    vs

    $ perl -e' use Try::Tiny; use feature qw( try ); { no feature qw( try ); try { }; } CORE::say "ok"; ' ok

    So I repeat: The order in which they are loaded is irrelevant, and what matters is if the feature is enabled or not when the try is encountered.

        In the OP's example Try::Tiny is activated first and can't know about a later feature.

        I think it can check correctly, but I'm not sure. If it can, it can do so regardless of whether it was loaded first or not. And if it can, it's not trivial.

        More importantly, is it even its job? After all, the following doesn't warn:

        sub print { ... } print ...;

        Maybe this is something Perl::Critic can handle.