perl - Is there a way to be notified when 'print' is called on $fh? -
when do:
print $fh 'text'; i need &sub called.
it there way that?
you can tie filehandle , customize behavior printing filehandle or other operation on filehandle.
sub printnotifier::tiehandle { ($pkg, $orignalhandle) = @_; bless { glob => $orignalhandle }, $pkg; } sub printnotifier::print { ($self,@msg) = @_; ... whatever want @msg here ... return print {$self->{glob}} @msg; } sub printnotifier::close { return close $_[0]->{glob} } open $fh, '>', 'some-file'; tie *$fh, 'printnotifier', $fh; print $fh "something"; # calls printnotifier::print
Comments
Post a Comment