Redirecting stdout without the use of IO::String

  • Thread starter Ilias Lazaridis
  • Start date
I

Ilias Lazaridis

I use the following code to redirect the stdout to a variable, which I
send then via email.

my $failure;
my $messages;
if ($notify_to_email) {

$self->output($messages); # currently not used

my $handle = IO::String->new($messages);
my $handle_old = select($handle); # redirect STDOUT

$failure = $self->check_tree($self); # prints to STDOUT,
either screen or $messages

select($handle_old) if defined $handle_old; # restore STDOUT
}else {
$failure = $self->check_tree($self);
}

$self->notify($failure, $notify_to_email, "", $messages);

The code depends on IO::String.

Can I achieve the above without the use of IO::String (and without
changing the programm structure much)?

..
 
M

Michele Dondi

I use the following code to redirect the stdout to a variable, which I
send then via email. [...]
Can I achieve the above without the use of IO::String (and without
changing the programm structure much)?

I didn't know about IO::String. Perl has been able to open() in memory
files for a while.

open my $fh, '>', \(my $string) or die "D'Oh! $!\n";

should do the job.


Michele
 
J

John W. Krahn

Ilias said:
I use the following code to redirect the stdout to a variable, which I
send then via email.

my $failure;
my $messages;
if ($notify_to_email) {

$self->output($messages); # currently not used

my $handle = IO::String->new($messages);
my $handle_old = select($handle); # redirect STDOUT

select() doesn't "redirect STDOUT", it changes the default output for print()
if no filehandle is used.

$failure = $self->check_tree($self); # prints to STDOUT,
either screen or $messages

select($handle_old) if defined $handle_old; # restore STDOUT
}else {
$failure = $self->check_tree($self);
}

$self->notify($failure, $notify_to_email, "", $messages);

The code depends on IO::String.

Can I achieve the above without the use of IO::String (and without
changing the programm structure much)?


my $failure;
my $messages;
if ( $notify_to_email ) {

$self->output( $messages );

open my $handle, '>', \$messages or die "Cannot open \$messages: $!";
my $handle_old = select $handle;

$failure = $self->check_tree( $self );

select $handle_old if fileno $handle_old;
} else {
$failure = $self->check_tree( $self );
}

$self->notify( $failure, $notify_to_email, '', $messages );




John
 
I

Ilias Lazaridis

Ilias said:
I use the following code to redirect the stdout to a variable, which I
send then via email.
[...]
my $handle_old = select($handle); # redirect STDOUT

select() doesn't "redirect STDOUT", it changes the default output for print()
if no filehandle is used.
ok

[...]
Can I achieve the above without the use of IO::String (and without
changing the programm structure much)?

my $failure;
my $messages;
if ( $notify_to_email ) {

$self->output( $messages );

open my $handle, '>', \$messages or die "Cannot open \$messages: $!";
my $handle_old = select $handle;

$failure = $self->check_tree( $self );

select $handle_old if fileno $handle_old;
} else {
$failure = $self->check_tree( $self );
}

$self->notify( $failure, $notify_to_email, '', $messages );

The code works fine.

Thanks a lot.

..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top