Perl Best Practices [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Perl Best Practices [Electronic resources] - نسخه متنی

Damian Conway

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







13.4. Systemic Failure


Be careful when testing for failure of the system builtin .


The system command is a particularly nasty case. Unlike most other Perl builtins, it returns false on success and true on failure. Fatal doesn't work on it either, so most people give up and write something like:


system $cmd
and croak "Couldn't run: $cmd ($OS_ERROR)";

The flow-of-control there is highly counterintuitive unless you're familiar with system's unusual failure return value.

A cleaner approach is to use the WIFEXITED ("if-exited") subroutine from the standard POSIX module:


use POSIX qw( WIFEXITED );

# And later...

WIFEXITED(system $cmd)
or croak "Couldn't run: $cmd ($OS_ERROR)";

Note that this particular return value anomaly will be fixed in Perl 6. The revised system function will still return an integer status value as in Perl 5, but the boolean value of that status will be "reversed": true if the status is zero and false otherwise. Those new semantics are already available in Perl 5, via the Perl6::Builtins CPAN module:


use Perl6::Builtins qw( system );

# and later...

system $cmd
or croak "Couldn't run: $cmd ($OS_ERROR)";


/ 317