...
This noncompliant code example opens a file, reads in its information, and closes it again.
| Code Block | ||||
|---|---|---|---|---|
| ||||
my $source; open(SOURCE, "<", $source); @lines = (<SOURCE>); close(SOURCE); |
...
This compliant colution does the same thing, but provides useful error messages if anything goes wrong.
| Code Block | ||||
|---|---|---|---|---|
| ||||
my $source; open(SOURCE, "<", $source) or croak "error opening $source: $!"; @lines = (<SOURCE>); close(SOURCE) or croak "error closing $source: $!"; |
...
EXP32-EX2: The autodie module is designed to replace functions that return a value indicating failure with functions that throw an exception on failure. When autodie is in use, then functions it redefined may be safely ignored.
| Code Block | ||||
|---|---|---|---|---|
| ||||
use autodie; my $source; open(SOURCE, "<", $source); @lines = (<SOURCE>); close(SOURCE); |
...