package GreppedFileHandle; use strict; sub TIEHANDLE { local *FH; open my $self, \*FH; bless $self, shift } sub PRINT { my $self = shift; my $output = "@_"; if( $output =~ /\Q$::GREP_STR\E/ ) { print( STDOUT $output ); } else { print "Swallowed a line\n"; }; } package main; our $GREP_STR = '123'; # needs to be 'our', not 'my' tie *grep_fh, 'GreppedFileHandle'; print STDOUT "Header - always print this\n"; print grep_fh "My line with 123\n"; ## printed at the terminal print grep_fh "My line with 456\n"; ## not printed