[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Redirection of STDOUT



At 5.48 -0600 2000.03.23, David Steffen wrote:
>Howdy!
>
>   I'm thinking about various ways that STDOUT can be redirected in
>MacPerl.  What I know about is:
>
>1) To a file
>2) To a socket
>2) To '>Dev:Printer', via a PAP connection (whatever that is).
>3) To '>Dev:Console:WindowName'
>
>   Is that all there is?
>
>   In Unix perl, one can redirect to any pipe, e.g. any other program
>on the computer, but that doesn't, in general work with a Mac.

It can work with ToolServer, though, I am thinking.

>Would
>the socket family PPC Toolbox substitute for this in some general and
>limited way using Apple Events?



>   I was thinking it would be nifty to be able to redirect STDOUT to a
>Perl function, but as I think about it and look through my books, I
>don't see that as an option.  Am I right?

I am not sure what you mean by this, but perhaps I do.  You can tie a
filehandle, capture printed output and do something with it later:

package Redirect;
sub TIEHANDLE {
    my $fh = [];
    bless $fh, __PACKAGE__;
}

sub PRINT {
    my $fh = shift;
    push @$fh, @_;
}

package main;
use Data::Dumper;
my $stdout = tie *STDOUT, 'Redirect';
print @INC;

# STDOUT is closed, print to STDERR
print STDERR Dumper($stdout);

__END__



You could also do something like this, passing a callback thingy to the tie:

package Redirect;
sub TIEHANDLE {
    my($pkg, $code) = @_;
    my $fh = $code;
    bless $fh, $pkg;
}

sub PRINT {
    my $fh = shift;
    $fh->(@_);
}

package main;
my $realstdout = *STDOUT;
my $stdout = tie *STDOUT, 'Redirect', \&process;

print join "\n", @INC;

sub process {
   for (@_) {
      s/(.)/ $1/g;
   }
   print $realstdout @_;
}

__END__

-- 
Chris Nandor       |     pudge@pobox.com      |     http://pudge.net/
Andover.Net        | chris.nandor@andover.net | http://slashcode.com/

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org