my $verbose = 1; my $r_grep = 'ssh %s grep "%s" "%s" |'; # host, re, file sub r_grep { my ($re,$spec) = @_; my ($host,$file) = split /:/, $spec,2; my $cmd = sprintf $r_cat, $host, $re, $file; print "Opening [$cmd]\n" if $verbose; open my $fh, $cmd or die "Couldn't open remote file: $cmd: $!/$?"; $fh }; my $fh = r_grep( 'foo|bar', 'user@host:/some/other/file' ); while (<$fh>) { chomp; die "Weird line <$_>" unless /foo|bar/; print "Read line [$_]\n"; };