in reply to Re: How portable are common $! error codes?
in thread How portable are common $! error codes?

>If you have to verify that something had the right result, then
>verify the result itself

Hmm.. perl doesn't make this easy though.

open() returns undef on failure, that's it.

Now that's not terribly useful - I have to treat a non-existant file separately to other errors, so I either have to check $! or test -f without introducing a race condition.

Ok, so I could go to something like this:


  use Errno;

  unless ( open(INFILE, "<", $filename) )
  {
    return if exists &Errno::ENOENT && $! + 0 == &Errno::ENOENT;
    return unless -f $filename;

    die "open failed: $!";
  }

Not great, but more portable. Makes use of Errno::ENOENT if its available but fallbacks to a simple -f as a last resort.

So, any suggestions for improvements???

  • Comment on Re: Re: How portable are common $! error codes?

Replies are listed 'Best First'.
Re: Re: Re: How portable are common $! error codes?
by Anonymous Monk on Apr 24, 2003 at 10:49 UTC
    Hmm, perhaps I'll just change -f to -e while nobody's looking :)
      If you are going to read it, check for that explicitly with '-r' rather than something like -f or -e.
Re: Re: Re: How portable are common $! error codes?
by Necos (Friar) on Apr 24, 2003 at 17:27 UTC
    If you're testing with -f, then (and you mentioned it) shouldn't you just combine that with the -e test? Like:
    if ( -e $file ) { #file exists if ( -f $file ) { #file is also plain do_your_thing(); } else { special_error_handler_for_non_plain_files(); } } else { #file doesn't exist special_error_handler_for_nonexistent_files(); }
    I know, a bit simplistic, but just throwing the idea out there for ya. YMMV.

    Theodore Charles III
    Network Administrator
    Los Angeles Senior High
    email->secon_kun@hotmail.com
    perl -e "map{print++$_}split//,Mdbnr;"