in reply to Problems with open

Uhm I've got perl 5.6.1 here on a win32-box (activestate perl) and it doesn't die when I have:
my $pager = "garbage"; open(FH,"| $pager") || die("Can't fork a $pager: $!"); print FH "Writing to $pager"; close(FH) || die("Can't close FH: $!");
and garbage doesn't exist. Also I get the PID returned but maybe this is a win32-issue.
You could simply do a check before the open, a la:
my $pager = "garbage"; if (-x $pager) { open(FH,"| $pager") || die("Can't fork a $pager: $!"); print FH "Writing to $pager"; close(FH) || die("Can't close FH: $!"); }


giant

Replies are listed 'Best First'.
Re: Re: Problems with open
by skerr1 (Sexton) on Jul 24, 2002 at 15:18 UTC
    I am using Perl 5.005_02 on a SunOS 5.6 machine. I thought of the -x pager but if the $pager does not contain the full path to whatever pager you are using (like less) then this is not going to work. I was hoping to find a cleaner way of doing this, but sounds like we are on the right track. I know we could always throw in a which, but like I said, I would like to find a cleaner way.

    Thanks,

    -sk
      I guess if you wanted to check if $garbage is a valid exe before executing it you could do something like the following. I'm not sure what the behavior is on 5.002 with File::Spec - can probably replace that with split(/:/,$ENV{'PATH'});
      use File::Spec; sub valid_exe { my $exe = shift; for my $dir ( File::Spec->path() ) { my $f = File::Spec->catfile($dir,$exe); if( -e $f && -x $f ) { # it is a valid exe return 1; } } return 0; }
      Essentially what which will do for you, your call if it is cleaner.