Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code example opens a file, reads in its information, and closes it again.

Code Block
bgColor#ffcccc
langperl
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
bgColor#ccccff
langperl
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
bgColor#ccccff
langperl
use autodie;

my $source;
open(SOURCE, "<", $source);
@lines = (<SOURCE>);
close(SOURCE);

...