Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Redirecting stdout without the use of IO::String

Reply
Thread Tools

Redirecting stdout without the use of IO::String

 
 
Ilias Lazaridis
Guest
Posts: n/a
 
      05-04-2007
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)?

..

--
http://lazaridis.com

 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      05-04-2007
On 4 May 2007 12:59:28 -0700, Ilias Lazaridis <(E-Mail Removed)>
wrote:

>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      05-04-2007
Ilias Lazaridis wrote:
> 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
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 
Reply With Quote
 
Ilias Lazaridis
Guest
Posts: n/a
 
      05-04-2007
On May 4, 11:23 pm, "John W. Krahn" <(E-Mail Removed)> wrote:
> Ilias Lazaridis wrote:
> > 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.

..

--
http://lazaridis.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
redirecting stderr and stdout Jon Landenburer Perl 1 05-13-2004 07:38 AM
redirecting stdout to ostream Alexander Stippler C++ 4 02-06-2004 09:03 AM
Ant redirecting stdout to a file while in a spawned java program Douwe Java 1 01-12-2004 02:16 AM
Re: redirecting stdin, stdout in java applications Sudsy Java 0 07-18-2003 09:34 PM



Advertisments