Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm working on some code where I'm piping my output to less and once the user views less and quits out of it, they will return to the program. However, on some larger files that are viewed in less, once you quit out it just exits back to the command prompt. But, if you page all the way down to the file till you see (END) and then quit out, it will bring the user back to the program. So my question is, how can I make it so whenever the user quits out of less it will ALWAYS bring them back to the program. Thanks

Replies are listed 'Best First'.
Re: pipe to less crash
by Corion (Patriarch) on Jul 01, 2011 at 18:49 UTC

      Okay, thanks. I now added a trap for SIGPIPE and I got an error "can't close: status = -1 at ...". So what do I from there? Also, here is code. Thanks again.

      $SIG{PIPE} = 'IGNORE'; open LESS, '|less' or die "unable to start pager"; print LESS $string or die "cant write: $!"; close LESS or die "can't close: status = $?"; system("clear");
        There was an error: You tried to write to a broken pipe.
        if (!print LESS $string) { die "cant write: $!" if !$!{EPIPE}; } close LESS;

        So, if you don't want your program to die, why do you use close or die? There are valid situations when close returns a false value, as you've found out, for example when the receiving end of the pipe has gone away.