Redirecting stdout without the use of IO::String

Discussion in 'Perl Misc' started by Ilias Lazaridis, May 4, 2007.

  1. 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
     
    Ilias Lazaridis, May 4, 2007
    #1
    1. Advertising

  2. On 4 May 2007 12:59:28 -0700, Ilias Lazaridis <>
    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,
     
    Michele Dondi, May 4, 2007
    #2
    1. Advertising

  3. 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
     
    John W. Krahn, May 4, 2007
    #3
  4. On May 4, 11:23 pm, "John W. Krahn" <> 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
     
    Ilias Lazaridis, May 4, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Landenburer

    redirecting stderr and stdout

    Jon Landenburer, May 12, 2004, in forum: Perl
    Replies:
    1
    Views:
    11,269
    Joe Smith
    May 13, 2004
  2. Sudsy
    Replies:
    0
    Views:
    4,410
    Sudsy
    Jul 18, 2003
  3. Douwe
    Replies:
    1
    Views:
    5,720
    Mike Schilling
    Jan 12, 2004
  4. Elad
    Replies:
    0
    Views:
    437
  5. Mark Volkmann

    redirecting stdout to a String

    Mark Volkmann, Mar 24, 2006, in forum: Ruby
    Replies:
    3
    Views:
    137
    James Edward Gray II
    Mar 24, 2006
Loading...

Share This Page