Project

General

Profile

Actions

action #176475

closed

coordination #154768: [saga][epic][ux] State-of-art user experience for openQA

coordination #176487: [epic] Up-to-date Perl stack

Use Feature::Compat::Try in our code - os-autoinst size:S

Added by tinita about 1 month ago. Updated 28 days ago.

Status:
Resolved
Priority:
Normal
Assignee:
Category:
Feature requests
Target version:
Start date:
2025-02-03
Due date:
% Done:

0%

Estimated time:

Description

Motivation

Using modern builtin perl features

https://metacpan.org/pod/Feature::Compat::Try is a module that provides the same
interface and functionality as the new try feature for older perl versions (<
5.34.0).

It automaticallly uses the builtin try feature for perl >= 5.34.0

use Feature::Compat::Try;

# In perl 5.34.0 same as:
use experimental 'try';

# Leap 16 will have perl 5.38, so as soon as we can make
# perl 5.38 the minimum version, we would write:
use v5.36; # strict, warnings, signatures, ...
use experimental 'try';

# With perl 5.40 it would be just:
use v5.40; # strict, warnings, signatures, try, ...

The basic try/catch feature got non-experimental with 5.40, so it's safe to
use.
The finally part is still experimental, so theoretically implementation
changes could happen.
The feature is tracked here: https://github.com/Perl/perl5/issues/18760

Example Feature::Compat::Try vs. eval and Try::Tiny

Feature::Compat::Try

use v5.12;
use warnings;
use experimental 'signatures';
use Feature::Compat::Try;

sub foo ($fail) {
    try {
        say "trying";
        die "oops" if $fail;
        return "pass"; # returns from foo()
    }
    catch ($e) {
        warn "Exception: $e";
        return "fail"; # returns from foo()
    }
    finally {
        say "whatever happens, I'm here";
    }
    say "end of subroutine"; # not reached
}

say foo(0);
say foo(1);

Try::Tiny

use v5.12;
use warnings;
use experimental 'signatures';
use Try::Tiny;

sub foo ($fail) {
    my $res;
    try {
        say "trying";
        die "oops" if $fail;
        $res = "pass";
        return "pass"; # does not return from foo()
    }
    catch {
        warn "Exception: $_";
        $res = "fail";
        return "fail"; # does not return from foo()
    }
    finally {
        say "whatever happens, I'm here";
    };
    return $res;
}

say foo(0);
say foo(1);

Pro

  • In Leap >= 15.5

Compared to Try::Tiny:

  • Future builtin
  • Can return from subroutine
  • catch ($some_name) instead of $_
  • Real block syntax element{} - no ; at the end needed

Con

  • Still partially experimental in 5.40

Related issues 3 (1 open2 closed)

Related to openQA Project (public) - action #133112: Switch unit tests to Test2-SuiteNew2023-07-20

Actions
Blocks openQA Project (public) - action #176319: testapi power function call "off" needs to be handled gracefully by os-autoinst size:SResolveddheidler

Actions
Copied to openQA Project (public) - action #176862: [beginner][easy] Use Feature::Compat::Try in our code - openQA size:SResolvedybonatakis

Actions
Actions #1

Updated by tinita about 1 month ago

  • Description updated (diff)
Actions #2

Updated by tinita about 1 month ago

  • Description updated (diff)
Actions #3

Updated by tinita about 1 month ago

Actions #4

Updated by okurz about 1 month ago

  • Assignee set to tinita
  • Target version set to Ready

As clarified with tinita she will check on the status of the finally part with someone from perl5porters and depending on the outcome we continue or leave more for later.

Actions #5

Updated by okurz about 1 month ago

  • Parent task set to #176487
Actions #6

Updated by tinita about 1 month ago ยท Edited

  • Description updated (diff)
  • Target version changed from Ready to future

So unfortunately finally seems to be still experimental. The issue is tracked here: https://github.com/Perl/perl5/issues/18760
It seems it's kind of the same implementation as for the defer feature, see https://metacpan.org/pod/Feature::Compat::Defer . (Which is also something that I would like to use in the future. For now we have Mojo:Util's scope_guard )

Actions #7

Updated by tinita about 1 month ago

Note that Try::Tiny provides finally as well, but we don't use it anywhere in our code, so we could switch to the try feature anyway right now.

Actions #8

Updated by okurz about 1 month ago

  • Status changed from New to In Progress
  • Assignee changed from tinita to okurz
  • Target version changed from future to Ready
Actions #9

Updated by okurz about 1 month ago

  • Blocks action #176319: testapi power function call "off" needs to be handled gracefully by os-autoinst size:S added
Actions #10

Updated by openqa_review about 1 month ago

  • Due date set to 2025-02-24

Setting due date based on mean cycle time of SUSE QE Tools

Actions #11

Updated by okurz about 1 month ago

https://github.com/os-autoinst/openQA/pull/6166 is how the corresponding code change would look like for openQA. I did not find relevant uses of Try::Tiny (or Test::Fatal) in related projects like openSUSE/qem-dashboard or os-autoinst-common, etc.

Actions #12

Updated by okurz about 1 month ago

  • Copied to action #176862: [beginner][easy] Use Feature::Compat::Try in our code - openQA size:S added
Actions #13

Updated by okurz about 1 month ago

  • Subject changed from Use Feature::Compat::Try in our code to Use Feature::Compat::Try in our code - os-autoinst
Actions #14

Updated by okurz about 1 month ago

2 PRs merged. Triggered https://app.circleci.com/pipelines/github/os-autoinst/openQA/15677/workflows/22743343-c15d-4a34-9075-39f2ebe49e39 which might bring us the updated dependency in openQA already otherwise we would need to wait until tomorrow

Actions #15

Updated by okurz about 1 month ago

  • Status changed from In Progress to Feedback
Actions #16

Updated by okurz about 1 month ago

https://github.com/os-autoinst/os-autoinst/pull/2652 merged. I would like to let the new dependency go through the whole pipelines. Then we can follow up with the rest.

Actions #17

Updated by okurz about 1 month ago

  • Status changed from Feedback to In Progress
Actions #19

Updated by okurz about 1 month ago

No bad effect from https://github.com/os-autoinst/os-autoinst/pull/2650 observed yet. Preparing some related improvement that I found while working on the code, e.g. using more Mojo::File.

Actions #21

Updated by okurz 30 days ago

  • Status changed from In Progress to Feedback
Actions #22

Updated by livdywan 29 days ago

  • Subject changed from Use Feature::Compat::Try in our code - os-autoinst to Use Feature::Compat::Try in our code - os-autoinst size:S

Stimated ;-)

Actions #23

Updated by okurz 28 days ago

  • Due date deleted (2025-02-24)
  • Status changed from Feedback to Resolved

https://github.com/os-autoinst/os-autoinst/pull/2663 merged. Deployed and also no bad feedback about that.

Actions

Also available in: Atom PDF