Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Capture::Tiny #2305

Closed
bschmalhofer opened this issue May 12, 2023 Discussed in #1042 · 1 comment
Closed

Using Capture::Tiny #2305

bschmalhofer opened this issue May 12, 2023 Discussed in #1042 · 1 comment
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@bschmalhofer
Copy link
Contributor

Discussed in #1042

Originally posted by bschmalhofer May 29, 2021
The issue https://github.com/RotherOSS/CodePolicy/issues/58 proposes that the CodePoliy should not complain about reopening STDOUT and STDERR. But maybe there is a better way.

This is current code:

sub _ITSM_ChangeDefinition {
    my ( $Self, %Param ) = @_;

    my $CommandObject = $Kernel::OM->Get('Kernel::System::Console::Command::Maint::ITSM::Configitem::DefinitionPerl2YAML');
    my $Success       = 0;

    my ( $Result, $ExitCode );

    {
        local *STDOUT;
        open STDOUT, '>:utf8', \$Result;    ## no critic qw(OTOBO::ProhibitOpen InputOutput::RequireEncodingWithUTF8Layer)
        $ExitCode = $CommandObject->Execute();
    }

    $Success = 1 if !$ExitCode;

    return $Success;
}

This code is fine, but kind of cryptic. An alternative is to use Capture::Tiny.

use Capture::Tiny qw(capture_stdout); 

sub _ITSM_ChangeDefinition {
    my ( $Self, %Param ) = @_;

    my $CommandObject = $Kernel::OM->Get('Kernel::System::Console::Command::Maint::ITSM::Configitem::DefinitionPerl2YAML');
    my $Success       = 0;

    my $ExitCode;
    binmode STDOUT, ':utf8'; # permanently
    my $Result = capture_stdout {
        $ExitCode = $CommandObject->Execute();
    };

    $Success = 1 if !$ExitCode;

    return $Success;
}

But permanently changing STDOUT is not nice. Is using Capture::Tiny really a good idea?

@bschmalhofer bschmalhofer added this to the OTOBO 11.0.1 milestone May 12, 2023
@bschmalhofer bschmalhofer self-assigned this May 12, 2023
bschmalhofer added a commit that referenced this issue May 12, 2023
bschmalhofer added a commit that referenced this issue May 12, 2023
Issue #2305 add Capture::Tiny as a required module
@bschmalhofer bschmalhofer added the enhancement New feature or request label May 12, 2023
@bschmalhofer
Copy link
Contributor Author

Capture::Tiny is now required and scripts/test/Console/Command/Help.t serves as an example of how to use the module. Thus Capture::Tiny can be used in future development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant