Redirect print stream from other class method

nanocv

Say I have a Parent and a Child classes. I can modify only Child class.

Parent class has some print's in it.

package Parent;

use strict;
use warnings;

sub new
{
    my $class = shift;
    my $self = {};
    bless $self, $class;
    return $self;
}

sub sayHello{
    my $self = shift;
    print "Hello world\n";
}

sub sayBye{
    my $self = shift;
    print "Bye world\n";
}

1;

In my Child class I have a method to print content into log files.

package Child;

use strict;
use warnings;

use parent 'Parent';

sub new
{
    my $class = shift;
    my $self = $class->SUPER::new();
    return $self;
}

sub talk{
    my $self = shift;
    $self->sayHello;
    $self->sayBye;
}

sub talkToLog{
    my $self = shift;

    my $logName = 'file.log';

    $self->log($logName, $self->sayHello); # Here's my best (not working) try
    $self->log($logName, $self->sayBye);
}

sub log{
    my $self = shift;
    my $filename = shift;
    my $string = shift;
    open(my $fh, ">>", $filename) || die "Couldn't open file $filename: $!";
    print $fh $string;
    close $fh;
    return;
}

1;

Question: Is there some way to "catch" the stream of those prints from Child class and log that content using the log method?


Here is the pl file I'm using to try.

#!/usr/bin/perl

use strict;
use warnings;

use Child;
my $child = Child->new;

$child->talk;
$child->talkToLog;

It prints this in console:

Hello world
Bye world
Hello world
Bye world
ilux

You need something like this:

sub talkToLog{
    my $self = shift;

    my $logName = 'file.log';
    my $output = '';

    open TOOUTPUT, '>', \$output or die "Can't open TOOUTPUT: $!";
    select TOOUTPUT;

    $self->sayHello;
    $self->log($logName, $output);

    $output = '';
    $self->sayBye;
    $self->log($logName, $output);

    select STDOUT;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Access to class method from other class method

Using method from one class within method for other class

python getattr from class method, why is it not finding the other class method?

JS - Calling method in class from other method in same class

Method from other class and use of toString()

How to call method from other class

Calling method from one class in other

Swift - Delegate not calling method from other class

Call a static method from other class in python

Issue starting method from other class

Mockito uses method stubbing from other test method in the same class

How to get access the method from other method of class?

Android getting parameter from one method to another method in other class

Redirect to a Controller from class method in ASP.NET MVC

ASP.NET MVC - Optionally Redirect from a Base Class Method?

coffeescript class - calling calling one class method from an other

Override method (append to other methods) in this class from another java class

How to call method in other class from a static class

Invoke a main class method from other class in Android

C++) How to use other class' method from a class?

Class Loop problem while calling method from other class

Class method points to other method of other class

class employee print method

Print text from a java class method within a .jsp page

Print out returned value from one method, to main class

undefined method error when pass function from one class to the other

How to verify if method was called from other with same class by mockito

Method invocation fails when class from other package

UML class diagram - Method from other classes - Which connector?