in reply to Re^2: print overloading
in thread print overloading

In its most simple case, you could do something like this:

#!/usr/bin/perl package MyHandle; sub TIEHANDLE { my $class = shift; return bless { @_ }, $class; } sub PRINT { my $self = shift; print STDERR "tied PRINT: @_\n"; # put your implementation here... } package main; tie *STDOUT, "MyHandle"; print "foo", "bar"; # "tied PRINT: foo bar" on STDERR

(But don't try to print "foo" (i.e. print to the tied STDOUT handle) within the PRINT() method itself — this might do nasty things like segfaulting...)

See Tying FileHandles for how to do it in a more complete way.

___

BTW, you could pass further arguments to the tie statement, e.g.

tie *STDOUT, "MyHandle", bar => 'quux';

These will be passed to the constructor (TIEHANDLE), which it turns into object attributes to utilize for whatever purpose you like. In the example, when dumping $self (using Data::Dumper), you'd then have

$VAR1 = bless( { 'bar' => 'quux' }, 'MyHandle' );