my $bogus; my $result; my $Rx = ( test => qr/abc(\d+)def/s ); my @cont = do{ local( *ARGV, $/, $_ ); @ARGV = $file; $/ = "arglbargl"; <>; }; for ( @cont ) { /$Rx{'test'}/ ? $result = $1 : $result = ++$bogus; print $result; }