Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6536821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:32:23+00:00 2026-05-25T10:32:23+00:00

The following is an excerpt from some code that I’m experimenting with: has buffer

  • 0

The following is an excerpt from some code that I’m experimenting with:

has buffer     => ( is => 'rw', isa => 'ScalarRef' );
has old_stdout => ( is => 'rw', isa => 'FileHandle' );

sub capture {
    my $self = shift;
    my $old_stdout;
    my $buffer;

    open $old_stdout, '>&', STDOUT
        or croak 'Cannot duplicate filehandle';

    close STDOUT;

    open STDOUT, '>', \$buffer
        or croak 'Cannot open filehandle';

    $self->old_stdout( $old_stdout );
    $self->buffer( \$buffer );
}

sub reset {
    my $self = shift;

    open STDOUT, '>&', $self->old_stdout
        or croak 'Cannot reset STDOUT';
}

Elsewhere:

my $stdout = Capture->new();
print "Some output\n";
$stdout->reset();

print $stdout->buffer();
# SCALAR(0x#######)

print ${$stdout->buffer()};
# Some output

I have tried cheating by setting the attribute hashref directly — it works, but I don’t like doing it:

    open STDOUT, '>', \$self->{buffer}
        or croak 'Cannot open filehandle';

I think I am missing something fundamental here. How should I go about storing $buffer (an in-memory file) as a Moose attribute, so that I can retrieve it later without the caller having to dereference it?

Update

I’ve added an around method modifier, which does the trick:

around 'buffer' => sub {
    my $orig = shift;
    my $self = shift;
    return ${$self->$orig} unless @_;
    $self->$orig( @_ );
};

…but it still feels messy. Is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:32:24+00:00Added an answer on May 25, 2026 at 10:32 am

    An option is to make both attributes (buffer and old_stdout) private by prepending underscore and then add simple method to access buffer from the outside:

    sub buffer {
        my $self = shift;
        return ${$self->_buffer}
    }
    

    It also look that you are trying to implement duplicate for Capture::Tiny or similar module.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following is an excerpt from some code I wrote to assign the $user->privilege
I have the following HTML (excerpt from larger code-base) <div class=diary-event ui-corner-all title=[title]> <span
In the following config file excerpt, the WCF service has two endpoints. <service behaviorConfiguration=AtomTcpHub.Behavior
Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)
I have some serial code like this that computes word concordances i.e. counting collocated
When I try to run the following code I get a Conversion from string
I needed some simple string encryption, so I wrote the following code (with a
I have some code that throws causes syncdb to throw an error (because it
I have the following XAML, that does all that it is supposed to, except
Greetings. I'm having troubles with the following legacy code. It's fine in everything except

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.