At 4:25 pm +0100 08/03/01, Bart Lateur wrote:
>On Wed, 7 Mar 2001 22:11:45 -0500, Morbus Iff wrote:
>
>>This is what I hope to accomplish. Can someone help me out?
>>
>> a) Redirect STDOUT to the window. Newlines need apply, and
>> autoscrolling would be helpful. I *do not* want to use
>> a Console window - I've already seen the trick of opening
>> a pipe to Dev:Console. Font doesn't matter.
>
>The way you can do something like this is with a tied filehandle. Create
>a package with appropriate subs for TIEHANDLE and PRINT, tie a typeglob
>("*FILE") to it, and print to this filehandle (it may be the default
>output handle, see select()). TIEHANDLE must simply return a properly
>blessed reference. For each print statement, the PRINT sub will be
>called. This one can then take the output-for-print, and paint it in the
>window. You'll have to take of newlines yourself, but in this package.
>See perltie ("Tied Objects", under "Advanced Topics") for more details.
>
>I'm sorry, I have virtually no experience in programming the Mac GUI
>from within MacPerl, so on that end, you mustn't expect any help from
>me; not even a working example.
It's an interesting idea which certainly hadn't crossed my mind and
which could be useful in certain circumstances.
There remains the problem of handing off the string to be displayed
to the MacWindow. In the case of MPEdit the string is handed to a
module with the syntax:
use Mac::Text::MPEdit;
MPE("Hello\n");
so you would have to replace all the instances of print() in the
script with MPE().
In the case of a tied file handle FOO you would need to replace all
instances of 'print()' with 'print FOO ()'.
The script below takes the example Bart quotes in "Tied Objects" and
draws the strings in a MacWindow. It's just by way of illustration,
and not to be thought of as anything more serious than that.
Alan Fry
#-----------
#!perl
my @str;
tie(*FOO,'Shout');
print FOO "hello\n";
$a = 4; $b = 6;
print FOO $a, " plus ", $b, " equals ", $a + $b, "\n";
print FOO "well I never...";
package Shout;
use Mac::Windows;
use Mac::Events;
use Mac::QuickDraw;
my($win, $bnd);
$bnd = Rect->new(10, 50, 260, 170);
$win = MacWindow->new($bnd, "Tiehandle Trial", 1, 8, 1);
$win->sethook('redraw', \&DoDraw);
WaitNextEvent while $win->window;
dispose $win;
sub TIEHANDLE {
my $i;
bless \$i, shift
}
sub PRINT {
shift;
push(@str, join($,,map(uc($_), @_)))
}
sub DoDraw {
my ($str) = @_;
my $line;
foreach (@str) {
$line++;
MoveTo(10, $line * 20);
DrawString($_)
}
}