开发者

Will this code work for other functions?

开发者 https://www.devze.com 2023-01-27 01:55 出处:网络
Does this kind of code work for other functions (instead of dbmopen) too, which opens a file and returns false when failing?

Does this kind of code work for other functions (instead of dbmopen) too, which opens a file and returns false when failing?

my $n_tries = 5;
while ($n_tries--) {
    if (dbmopen (%CHOICES, &dbm_file(), 0600开发者_运维知识库)) {
        last;
    } else { 
        if ($! eq 'Resource temporarily unavailable') {
            my $wait = rand 0.45; select undef, undef, undef, $wait;
        } else { return;
        }
    }
}


It's a pattern that can apply in other cases -- but generally you don't want to check the string value of $!, because it's dependent on the system libraries, the user's locale, and things like that. When dealing with system errors it's best to work with the error numbers themselves, and perl has a way to do that -- in short, you can do a check like if ($!{EAGAIN}) { ... } (that's a lookup in the hash %!) and the value will only be true if $! is set to the error number of EAGAIN. To be compatible with older perls you will need to load the module Errno -- it seems to work out-of-the-box on 5.8 and newer, but just loading that module ensures that it works everywhere.

There are, of course, some fiddly bits involved even in numeric error numbers, as every platform might not have certain error numbers. You can check whether or not a given name (like EAGAIN) has an error number on the current system by seeing if that key exists in %!, and then figure out where to go from there -- but it's still more reliable than string comparison.


I’d say it does not, because you rely on $! being set to the Resource … string, which I don’t think is a standard for the open functions. What exactly are you trying to do?

0

精彩评论

暂无评论...
验证码 换一张
取 消